“没有路线[POST]匹配”改变的link_to到button_to时(“No route matc

2019-07-31 16:53发布

我有这样的代码:

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

它工作正常,但是当我将其更改为,

<%= button_to "New User", new_user_path, :class => "button"  %><br />

我得到这个错误

无路由匹配[POST]“/用户/新”

任何帮助都将不胜感激。

Answer 1:

耶稣罗德里格斯是正确的约POST和GET,但如果你真的需要的按钮,你可以简单地覆盖默认的方法:

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


Answer 2:

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.



Answer 3:

而不是强迫button_to使用非默认的方法,你也可以发送一个类的link_to。

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


Answer 4:

button_to默认为POST和的link_to默认为GET,这就是为什么links_to工作。 您可以强制button_to使用GET:

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

你可以在这里获取有关button_to选项的详细信息: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to



文章来源: “No route matches [POST]” when changing link_to to button_to