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

泄露秘密 提交于 2020-01-05 14:02:35

问题


Please help me for resolve this error.

Error: No route matches [POST] "/users/new"

My code snippets are:

views/users/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

    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.


回答1:


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:

<%= 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.




回答2:


The default method type for button_to is POST. You have posts_new_path as type GET.

So better you use link_to instead of button_to and use css to convert these link to look like button.

<%= link_to "REGISTER",users_new_path%>

css should be like:

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



回答3:


The default for button_to is POST, You can pass method: :get as third argument to make it work for your route

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

You don't have to mention controller and action, just define your routes as

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


来源:https://stackoverflow.com/questions/27519857/no-route-matches-post-users-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!