Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~

Introduction

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 ~

About Rails naming conventions

So far, we've created controllers and models.

They have a naming convention. Let's learn.

Model naming convention

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`.

View naming convention

The View folder has multiple files under it, so it is plural.

Controller naming convention

Since the Controller has multiple actions, create it in the plural.

Register data in database

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.

image.png

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.

Redirect when you press send

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.

At the end

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

Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Explanation of Ruby on rails for beginners ①
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
How to use Ruby on Rails
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use kaminari
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby on Rails] How to use session method
How to build a Ruby on Rails environment using Docker (for Docker beginners)
(For beginners) [Rails] Time saving tech! How to install and use slim
[For Rails beginners] Summary of how to use RSpec (get an overview)
(Ruby on Rails6) How to create models and tables
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Ruby On Rails] How to search and save the data of the parent table from the child table
[RSpec on Rails] How to write test code for beginners by beginners
[Ruby on Rails] How to avoid creating unnecessary routes for devise
[Ruby on Rails] About bundler (for beginners)
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby On Rails] How to search the contents of params using include?
[Rails] Articles for beginners to organize and understand the flow of form_with
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Ruby] How to use gsub method and sub method
Ruby on Rails ~ Basics of MVC and Router ~
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to install Bootstrap in Rails
[For super beginners] How to use autofocus: true
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to use Eclipse on my PC with 32bit and 2GB of memory
How to implement login request processing (Rails / for beginners)
[Ruby on Rails] How to write enum in Japanese
Rails / Ruby: How to get HTML text for Mail
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to reset DB in Heroku
How to use GitHub for super beginners (team development)
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once
[Rails] How to use enum
How to use Ruby return
[Rails] How to use enum
How to use rails join
Ruby: How to use cookies
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[For beginners] Explanation of classes, instances, and statics in Java
[For Ruby beginners] Explain how to freely delete array elements!
[Ruby on Rails] Elimination of Fat Controller-First, logic to model-
How to use Font Awesome icon for ul and li
How to display a graph in Ruby on Rails (LazyHighChart)
How to run React and Rails on the same server
Procedures for passing RealmObject to Fragment and how to use Parceler
How to deal with different versions of rbenv and Ruby
[Rails] How to display error messages for comment function (for beginners)
[Rails] How to use devise (Note)