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
.
If you implement it with `` `form_tag```, which is often used in Progate, it will be very handmade.
As for the flow,
<%= 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.
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
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.
-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