[After learning Progate] I tried to summarize form_with while comparing with form_tag

Many people will move their learning from Progate to Rails tutorials.

Form-related is difficult even in Progate. It's really difficult around here to sign up or log in using the form.

However, Progate is still written in an easy-to-understand manner.

Create a form with form_tag, send data with , and receive the data with params. Very easy to understand.

However, when I started learning Rails in earnest, I stopped using form_tag and used form_with. The amount of Rails description is drastically reduced from this area. Understanding will also decrease dramatically.

This article is for people who are following a learning route like the Rails tutorial from Progate and who just can't understand form_with.

I will write while comparing how to write Progate and how to write Rails.

In conclusion, I think that the writing style of Progate is aimed at "consistent writing style" for beginners. Originally, when there is a change in the database, form_tag is not used. Use form_for.

Progate uses the form_tag helper and input tags to create a handmade feel.

If you implement it with `` `form_tag```, which is often used in Progate, it will be very handmade.

As for the flow,

  1. Send data to the controller with form_tag and input tag.
  2. Receive data with create action
  3. Save and write to database
<%= form_tag("/users/create") do %>
  <p>username</p>
    <input name="name" value="<%= @user.name %>">
  <p>mail address</p>
    <input name="email" value="<%= @user.email %>">
    <input type="submit" value="sign up">
 <% end %>

Set the path with form_tag. Set the path to / users / create. At this time, in the routing (routes.rb),

This is written, isn't it? So the create action in users_controller.rb will be triggered. The contents of the create action are as follows.

  def create
    @user = User.new(name: params[:name], email: params[:email])
    if @user.save
      flash[:notice] = "User registration is complete"
      redirect_to("/users/#{@user.id}")
    else
      render("users/new")
    end
  end

It looks like this.

The flow is easy to grasp, isn't it? I receive what I sent from the form as params and save it using @ user.save, so I can understand each one logically.

If you proceed to the Rails tutorial from here, you will no longer use form_tag.

This is because the way Progate is written is exceptional.

Use form_for when the database is involved

I don't use how to write a progate. form_tag is not used when newly registering, editing, or deleting in the database.

When editing the database, use form_for.

Take a model object (an object (instance) created based on the User model, such as @ user = User.new) as an argument.

At the time of form_tag, I was doing the troublesome thing of fetching id information from: id, finding the record from the database, and editing that information.

There was such a process.

However, the great thing about Rails is that @user also automatically has a user ID.

Rails is heavily black-boxed, so

Specify the destination with form_tag.

You used form_tag when using form-related helpers in Ruby on Rails in Progate.

At that time, I got stuck in the omission. Progate writes everything anyway. Form

What is form_with?

form_with is a combination of form_tag and form_for. form_tag specified the path. form_for specified a model object (an instance created from a model).

It is form_with that you can use both of them.

form_tag

<%= form_tag 'path' do %>
  <input ...
  <input ...
<% end %>

In contrast to writing

form_for is

<%= form_for(@user) do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

As you can see, it's annoying to write differently even though the form is similar. By making all form_with and trying to unify the writing style, it became one.

However, those who have learned Progate have learned form_tag, so

"What? Is it a model as an argument of form?"

I'm confused. We've set the action and came.

The great thing about this is that form_with model: @user and model are selfish.

When you do it with form_tag, you specified the action.

<% = form_tag ("/ users / create") do%> This is the part. The create action is specified.

However, when writing model: @user,

When registering as a new user, It will call a new action

In this way, it changes the action to be called with care. Rails has a lot of features that save you time and effort.

First, you will receive: id information from the URL. Then, identify the user from the id information

This: All the work of fetching the id is unnecessary. In fact, Rails does everything automatically. However, Progate skipped everything in the initial learning,

"I'm not sure, but Rails will do it all."

It's hard to understand, so he explains it well.

Summary

-Form_tag is used when there is no change in the database ・ Form_for is used when creating a form that involves updating the database. ・ Form_with can play both roles

Rails is really smart. It will do various things without permission.

"I finally did it without being told."

I will say. But at the beginning

"Why did you decide that?"

You may be wondering. I'm still confused by Rails's wittyness.

However, I don't think there is anything more convenient and reliable when you can master it. Let's do our best so that we can master it.

Recommended Posts

[After learning Progate] I tried to summarize form_with while comparing with form_tag
I tried to summarize Java learning (1)
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to summarize Java 8 now
After learning Progate, I tried to make an SNS application using Rails in the local environment
I tried to summarize the methods used
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried time-saving management learning with Studyplus.
I tried to summarize the Stream API
What is Docker? I tried to summarize
I tried to implement ModanShogi with Kinx
I tried to summarize about JVM / garbage collection
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make Basic authentication with Java
I tried to manage struts configuration with Coggle
I tried to manage login information with JMX
[Must see !!!] I tried to summarize object orientation!
I tried to break a block with java (1)
I tried to summarize the points to consider when acquiring location information with the iOS application ③
I tried to summarize the points to consider when acquiring location information with the iOS application ①
I tried to summarize the points to consider when acquiring location information with the iOS application ②
I tried what I wanted to try with Stream softly.
I tried to implement file upload with Spring MVC
I tried to read and output CSV with Outsystems
[Java 11] I tried to execute Java without compiling with javac
I tried to summarize the state transition of docker
I started MySQL 5.7 with docker-compose and tried to connect
I tried to get started with Spring Data JPA
[Machine learning] I tried Object Detection with Create ML [Object detection]
I tried to summarize various link_to used this time
I tried to draw animation with Blazor + canvas API
I tried to implement Stalin sort with Java Collector
roman numerals (I tried to simplify it with hash)
I tried to create a java8 development environment with Chocolatey
I tried to increase the processing speed with spiritual engineering
I tried to summarize the basics of kotlin and java
[Rails] I tried to create a mini app with FullCalendar
I tried to summarize the basic grammar of Ruby briefly
I tried to link chat with Minecraft server with Discord API
I tried to automate LibreOffice Calc with Ruby + PyCall.rb (Ubuntu 18.04)
I tried to summarize personally useful apps and development tools (Apps)
I tried to create a padrino development environment with Docker
I tried to get started with Swagger using Spring Boot
I tried to summarize object orientation in my own way.
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried to be able to pass multiple objects with Ractor
I tried to express the result of before and after of Date class with a number line
I tried to make a machine learning application with Dash (+ Docker) part2 ~ Basic way of writing Dash ~
I tried DI with Ruby
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried to verify yum-cron
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to summarize the words that I often see in docker-compose.yml
I tried to summarize what was asked at the site-java edition-
I tried to build the environment of PlantUML Server with Docker
I tried connecting to MySQL using JDBC Template with Spring MVC
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]