“No route matches [POST]” when changing link_to to button_to

前端 未结 4 672
余生分开走
余生分开走 2021-02-04 04:45

I have this piece of code:

<%= link_to \"New User\", new_user_path, :class => \"button\"  %>

which works fine, but when I

相关标签:
4条回答
  • 2021-02-04 04:50

    button_to defaults to POST, and link_to defaults to GET, this is why links_to worked. You can force button_to to use GET:

    <%= button_to "New User", new_user_path, :class => "button", :method => :get %>
    

    You can get more information about button_to options here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

    0 讨论(0)
  • 2021-02-04 05:06

    Instead of forcing button_to to use a non-default method, you can also send a class to link_to.

    <%= link_to "New User", new_user_path, :class => "button" %>
    
    0 讨论(0)
  • 2021-02-04 05:12

    The "link_to" is looking for a /users/new using GET.

    The "button_to" is looking for a /users/new using POST

    If you create the routes for a controller using:

    resources :user
    

    By default, /users/new is a GET and not POST so, the second line doesn't find any route.

    If you are thinking to change that action to POST I think that you should forget about it.

    0 讨论(0)
  • 2021-02-04 05:13

    Jesus Rodriguez is right about POST and GET, but if you really need the button you can simply override the default method:

    <%= button_to "New User", new_user_path, :class => "button", :method => :get  %>
    
    0 讨论(0)
提交回复
热议问题