I am creating an original app with Rails. I wanted to execute the update action directly from the page displayed by the index action, so I used the button_to method.
Development environment ruby 2.6.5 Rails 6.0.3.4
Similar to the commonly used link_to, you can create buttons instead of links. It's simple and consists of button names, paths, actions and options. The default HTTP method is post.
ruby:○○.html.erb
<%= button_to "Button name", {Path or controller name and action name}, {option} %>
The ones I created this time are as follows. suggestion_path (suggestion.id) is the path set in the update routing. The HTTP method of the update action is patch, so you need to change it from the default post.
ruby:index.html.erb
<%= button_to "Cleaning completed", suggestion_path(suggestion.id), method: :patch, class: "register-blue-btn" %>
The default HTTP method for link_to is get. Also, link_to creates a link using the a tag, while button_to creates a form.
Reference page [Rails] Explains how to use button_to in an easy-to-understand manner! that's all
** Impression ** I still don't understand this area properly, but if you want to perform an action, is it button_to? Since you can also specify the controller, it seems that you can also execute actions of other controllers.
Recommended Posts