This time is a continuation of the previous article.
Please see the previous article if you like.
Explanation of Ruby on rails for beginners ① Explanation of Ruby on rails for beginners ② ~ Creating links ~ Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
So far, we've created controllers and models.
They have a naming convention. Let's learn.
A model is a blueprint for creating a database.
Since there is only one blueprint, create the model class name in singular
.
The table is automatically represented by the plural
because it has multiple data for that model.
In the previous article, I created a model with the name ʻuser`.
This will create a table with the name ʻusers`.
Also, the class name starts with an uppercase letter, such as ʻUser`.
The View folder has multiple files under it, so it is plural
.
Since the Controller has multiple actions, create it in the plural
.
In the previous article, I used the rails console to register data in the database.
This time, let's register the data in the database by letting the user operate the browser.
Before that, check the database once.
Type the following command in the terminal.
rails dbconsole
You can turn on the display of column names with the following code.
sqlite> .headers on
In this state, you can check the contents by writing the following SQL statement.
sqlite> select * from users;
id|name|password|created_at|updated_at
1|poco|maru|2020-05-20 10:50:13.177731|2020-05-20 10:50:13.177731
You can see that the users table now contains the name and password columns.
Now let's create an input form and store the data sent by the user in the database.
Write the following code in the /users/new.html.erb file.
new.html.erb
<%= form_tag("/users/create") do %>
<p>username</p>
<input name="name">
<p>password</p>
<input name="password">
<input type="submit" value="Send">
<% end %>
form_tag
is used to send or delete some value from the view file to the controller. This is the so-called POST request
. For the difference between get request and post request, please refer to the article here.
When a user sends a button with type =" submit "
, a post request corresponding to ʻusers / create` is executed.
This time, the routing is as follows.
routes.rb
post "users/create" => "users#create"
That is, the above data is sent to the create action of the users controller. In the create action, write the following code.
users_controller.rb
def create
user = User.new(name: params[:name], password: params[:password])
user.save
end
This code uses the User class of the user model to store data in the users table of the database.
In the data sent by form_tag, if there is a tag with name attribute specified, the value can be handled as params [name attribute]
in the controller.
In this case, the value in <input name =" name ">
is stored in params [: name]
and the value in <input name =" password ">
is stored in params [: password].
A model is created by using ʻUser.new (name: params [: name], password: params [: password])` for the sent data and stored in the database.
From the user, it is as follows.
Let's press Send
in this state. Then, in the create action of the users controller, the string this is stored in params [: name] and the string test is stored in params [: password].
I was able to store the data as follows.
3|this|test|2020-05-21 05:30:36.718523|2020-05-21 05:30:36.718523
However, if you leave it as it is, there is no particular change even if you press send. It's boring, so let's redirect to another file.
When you press send, the create action of the users controller is executed, so if you write the redirect code in this create action, you will be redirected to another file.
Let's add the code to the users controller as follows.
users_controller.rb
def create
user = User.new(name: params[:name], password: params[:password])
user.save
redirect_to("/users/index")
end
If you write it like this, you will be redirected to the file corresponding to / users / index
when you press send.
By the way, when writing redirects, don't forget to put /
at the beginning of the path.
That's all for this article.
Thank you for your hard work.
Please see the following articles if you like.
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ⑦ ~ Implementation of flash ~
Recommended Posts