I have this piece of code:
我有这段代码:
<%= link_to "New User", new_user_path, :class => "button" %><br />
which works fine, but when I change it to,
哪个工作正常,但当我改成它时,
<%= button_to "New User", new_user_path, :class => "button" %><br />
I get this error
我收到这个错误
No route matches [POST] "/users/new"
没有路由匹配[POST]“/ users / new”
Any help at all will be appreciated.
任何帮助都将不胜感激。
4 个解决方案
#1
30
Jesus Rodriguez is right about POST and GET, but if you really need the button you can simply override the default method:
Jesus Rodriguez对POST和GET是正确的,但是如果你真的需要按钮,你可以简单地覆盖默认方法:
<%= button_to "New User", new_user_path, :class => "button", :method => :get %>
#2
19
The "link_to" is looking for a /users/new using GET.
“link_to”正在使用GET查找/ users / new。
The "button_to" is looking for a /users/new using POST
“button_to”正在使用POST查找/ users / new
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.
默认情况下,/ users / new是GET而不是POST,因此第二行找不到任何路由。
If you are thinking to change that action to POST I think that you should forget about it.
如果您打算将该操作更改为POST,我认为您应该忘记它。
#3
3
Instead of forcing button_to to use a non-default method, you can also send a class to link_to.
您也可以将类发送到link_to,而不是强制button_to使用非默认方法。
<%= link_to "New User", new_user_path, :class => "button" %>
#4
2
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默认为POST,link_to默认为GET,这就是links_to工作的原因。您可以强制button_to使用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
您可以在此处获取有关button_to选项的更多信息:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
#1
30
Jesus Rodriguez is right about POST and GET, but if you really need the button you can simply override the default method:
Jesus Rodriguez对POST和GET是正确的,但是如果你真的需要按钮,你可以简单地覆盖默认方法:
<%= button_to "New User", new_user_path, :class => "button", :method => :get %>
#2
19
The "link_to" is looking for a /users/new using GET.
“link_to”正在使用GET查找/ users / new。
The "button_to" is looking for a /users/new using POST
“button_to”正在使用POST查找/ users / new
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.
默认情况下,/ users / new是GET而不是POST,因此第二行找不到任何路由。
If you are thinking to change that action to POST I think that you should forget about it.
如果您打算将该操作更改为POST,我认为您应该忘记它。
#3
3
Instead of forcing button_to to use a non-default method, you can also send a class to link_to.
您也可以将类发送到link_to,而不是强制button_to使用非默认方法。
<%= link_to "New User", new_user_path, :class => "button" %>
#4
2
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默认为POST,link_to默认为GET,这就是links_to工作的原因。您可以强制button_to使用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
您可以在此处获取有关button_to选项的更多信息:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to