开发者

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 'Edi开发者_运维知识库t 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.


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


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 %>


Ruby -v 2.8.6, Rails 6.1.4.1

<%= button_to 'Edit', edit_item_path(item), :method => :get %> because with expression (@item) you do not define the object you want to edit, because (@item) it is not a specific object, there are several and you need to define only the one you want to edit, :method => :get this method is perfect

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜