ruby-on-railsrubybuttonlink-to

Button_to in Ruby on Rails bad route


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.


Solution

  • 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).