Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 455
Nachshon Schwartz Avatar asked Jan 16 '11 22:01

Nachshon Schwartz


2 Answers

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

like image 162
Peter Brown Avatar answered Nov 20 '22 04:11

Peter Brown


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 %>
like image 2
pinku Avatar answered Nov 20 '22 05:11

pinku