没有路线匹配[POST]" / users / new"

时间:2014-12-17 06:54:03

标签: ruby-on-rails ruby regex ruby-on-rails-3

请帮我解决此错误。

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

我的代码段是:

视图/用户/ 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>

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

请帮我设置路线文件。

3 个答案:

答案 0 :(得分:2)

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

# 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 %>

请注意大括号。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

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

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