[Rails] I tried using the button_to method for the first time

Introduction

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

table of contents

  1. How to use button_to
  2. Difference from link_to

1. How to use button_to

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

image.png

2. Difference from link_to

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

[Rails] I tried using the button_to method for the first time
I tried using Docker for the first time
I tried touching Docker for the first time
I tried using the Migration Toolkit for Application Binaries
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
Spring Boot for the first time
I tried to explain the method
[Rails] I tried deleting the application
[Rails 6] destroy using the resources method
Spring AOP for the first time
Think when Rails (turbolinks) doesn't load the page for the first time
Java14 came out, so I tried record for the time being
I tried to understand how the rails method "redirect_to" is defined
I tried to understand how the rails method "link_to" is defined
Introduction to java for the first time # 2
Learning for the first time java [Introduction]
Use the l method for time notation
Java12 came out, so I tried the switch expression for the time being
[For beginners] I tried using DBUnit in Eclipse
I tried barcode scanning using Rails + React + QuaggaJS
[Rails] I tried to raise the Rails version from 5.0 to 5.2
[For beginners] I tried using JUnit 5 in Eclipse
I tried to organize the session in Rails
Walls hit by Rspec for the first time
[API] I tried using the zip code search API
Android Studio development for the first time (for beginners)
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
I summarized the naming conventions for each Rails
I tried using the profiler of IntelliJ IDEA
The story of intentionally using try catch for the first time in my life
Impressions and doubts about using java for the first time in Android Studio
[JDBC ③] I tried to input from the main method using placeholders and arguments.
Ideal and reality that I felt when I used Optional for the first time ~ Implementation of cache using Map ~
I tried using Gson
I tried using TestNG
I tried using Galasa
I tried using the Server Push function of Servlet 4.0
[Rails] I tried playing with the comment send button
What should I use for the testing framework [Rails]
Learn for the first time java # 3 expressions and operators
I tried using Docker Desktop for Windows on Windows 10 Home
Oreore certificate https (2020/12/19) for the first time with nginx
I tried using an extended for statement in Java
Learning memo when learning Java for the first time (personal learning memo)
I want to call the main method using reflection
Command to try using Docker for the time being
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
[Must-see for beginners] I changed the display of the posting time to Japanese time [l method]
[CircleCI] I will explain the stupid configuration file (config.yml) that I wrote for the first time.
I tried unit testing Rails app using RSpec and FactoryBot
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
I tried using the GitHub repository as a library server
about the where method (rails)
I tried using azure cloud-init
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried Rails beginner [Chapter 1]
I tried the Docker tutorial!
I tried using Apache Wicket
I tried the VueJS tutorial!
I tried to build the environment little by little using docker