Button_to in Ruby on Rails bad route

I am trying to use the button_to rails button_to . I wrote the following code:

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

and received 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 will load without errors. The meaning of this code is:

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

loading normally. Perhaps there is some kind of button_to feature that I don’t know about, but I'm losing.

+6
ruby ruby-on-rails button link-to
source share
2 answers

I think you can use button_to incorrectly. I always thought that if you are referencing an edit action, you should use link_to. The buttons seem to be for actions that need to send / put data, such as updating a form or deleting a record.

Update:

By default, button_to uses POST instead of GET. Therefore, it works when you just visit the url (i.e. GET).

+5
source share

button_to is POST by default, and link_to is GET by default.

If you really need a button, you can change the default method for GET for editing and other links.

for ex:

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

All Articles