Button_to in Ruby on Rails bad route

南笙酒味 提交于 2019-12-09 18:09:30

问题


I'm trying to use the button_to rails helper. I wrote the following code:

<%= button_to 'Edit Item', edit_item_path(@item), :class => 'mark-button' %>

and got the following error message

No route matches "/items/1/edit"

But when I refresh the page it goes to the appropriate action. The URL of the page i get is localhost:3000/items/1/edit which is the correct URL. If I switch the button_to command to link_to the page loaded with no errors. Meaning this code:

<%= link_to 'Edit Item', edit_item_path(@item), :class => 'mark-button' %>

loads fine. Maybe there is some feature of button_to I'm not aware of, but I am at a lost.


回答1:


I think you might be misusing button_to. I've always thought that if you're linking to the edit action, you should be using link_to. Buttons seem to be for actions that need to post/put data such as updating a form or deleting a record.

Update:

By default, button_to uses POST instead of GET. Hence it working when you just visit the URL (ie GET).




回答2:


button_to defaults to POST, and link_to defaults to GET.

If you really need button_to you can change the default method to GET for edit and other links.

for ex:

<%= button_to 'Edit', edit_user_path(@user), :method => :get %>


来源:https://stackoverflow.com/questions/4708671/button-to-in-ruby-on-rails-bad-route

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