简体   繁体   中英

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

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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