没有路由匹配“/ users / new”

请帮我解决这个错误。

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

我的代码片段是:

意见/用户/ index.html.erb

This is Registration page..

"users", :action => "new",method: :get} %>

配置/ 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 

请帮我设置路线文件。

首先,通过命名路线让您的生活更轻松:

 # 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 

另外请注意:method参数是第三个(非第二个)参数的一部分:

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

或者没有指定的路线:

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

注意大括号。

button_to的默认方法类型是POST 。 你有posts_new_path作为类型GET

所以你使用link_to而不是button_to更好,并使用css将这些链接转换为按钮。

 <%= link_to "REGISTER",users_new_path%> 

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; } 

button_to的默认值是POST ,您可以传递method: :get作为第三个参数,使其适用于您的路线

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

您不必提及controlleraction ,只需将路由定义为

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