简体   繁体   English

没有路由匹配[POST]“ / users / new”

[英]No route matches [POST] “/users/new”

Please help me for resolve this error. 请帮助我解决此错误。

Error: No route matches [POST] "/users/new" 错误:没有路由匹配[POST]“ / users / new”

My code snippets are: 我的代码段是:

views/users/index.html.erb 意见/用户/ index.html.erb

<h1>This is Registration page..</h1>
<div class="sign">
<%= button_to "Registration",{:controller => "users", :action => "new",method: :get} %>
<%= button_to "Login",action:"login" , method: :get %>
</div>

config/route.rb 配置/ route.rb

    Rails.application.routes.draw do
      root "users#index"
      get "users/new" => "users#new"
      get "users/login" => "users#login"
      #get "users/new" => "users#new", as: new_user
      #get "users/login" => "users#login", as: login_user
   end

Please help me to setup the routes file. 请帮助我设置路由文件。

First make your life easier by naming your routes: 首先,通过命名路线来使您的生活更轻松:

# in config/routes.rb
Rails.application.routes.draw do
  root "users#index"
  get "users/new"   => "users#new",   :as => :new_user
  get "users/login" => "users#login", :as => :login
end

Furthermore notice that the :method argument is part of the third (not second) argument: 此外,请注意:method参数是第三个(不是第二个)参数的一部分:

<%= button_to "Registration", new_user_path, :method => :get %>
<%= button_to "Login", login_path, :method => :get %>

Or without the named routes: 或不带命名路线:

<%= button_to "Registration", { :controller => "users", :action => "new" }, :method => :get %>
<%= button_to "Login", { :action => "login" }, method => :get %>

Note the curly brackets. 请注意大括号。

The default method type for button_to is POST . button_to的默认方法类型是POST You have posts_new_path as type GET . 您具有类型为GET posts_new_path

So better you use link_to instead of button_to and use css to convert these link to look like button. 因此最好使用link_to而不是button_to并使用css将这些链接转换为类似于button的形式。

<%= link_to "REGISTER",users_new_path%>

css should be like: CSS应该是这样的:

a:link, a:visited {
  display: block;
  width: 6em;  
  padding: 0.2em;
  line-height: 1.4;
  background-color: #94B8E9;
  border: 1px solid black;
  color: #000;
  text-decoration: none;
  text-align: center;
}

a:hover {
 background-color: #369;
 color: #fff;
}

The default for button_to is POST , You can pass method: :get as third argument to make it work for your route button_to的默认值为POST ,您可以传递method: :get作为第三个参数,使其适用于您的路线

<%= button_to "Registration", new_user_path, method: :get %>

You don't have to mention controller and action , just define your routes as 您不必提及controlleraction ,只需将您的路线定义为

get "users/new" => "users#new", :as => :new_user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM