[Ruby] Chewing the Rails Tutorial [Chapter 2 Toy Application]

4 minute read

2.1 Application planning

■ rails generate There are four usages below.

  1. rails generate controller → Generate controller, view and routing
  2. rails generate model → Generate model and migration file
  3. rails generate migration → Generate migration file
  4. rails generate scaffold → Generate all (controller, view, model, migration file, routing).

The following explanation is easy to understand https://diveintocode.jp/blogs/Technology/RailsGenerateCommand

■ Migration Mechanism to operate DB table based on migration file. You can mess with the DB without writing SQL statements directly. (Table can be created)

■ Migration file Design drawing when creating DB. Script file. It is reflected in the DB by executing rails db:migrate.

2.2 Users resource

■ rails generate scaffold User name:string email:string Create a table called users on the database. The table has id, name and email. means. This will generate users_controller.rb.

■ resources :users corresponds to users index create new edit show update destroy Are all created. “:” is a symbol.

■ Symbol It’s a spell to make Ruby faster.

[Practice] Just try 1, 2, 3, 4 all.

■ @users = User.all Variables starting with @ are called instance variables. Declare it in the controller. If you declare it, you can use it in a view.


  1. It’s troublesome to make a diagram, so it’s a pass.
  2. Looking at the behavior shown, look for the code that retrieves user information from the database in the code generated by Scaffold. → It is the model that is related to the database. But the model created this time ・Application_record.rb ・User.rb No applicable code for. Then, the controller side looks for the part related to the model. There aren’t many places where @user= is written.


# Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])

Create a method called set_user. I’m looking for a user in User.find. find () is a method to search the model. params is also a method. You can call the contents with params[:column name]. Here, we call the id in the model.

  1. What is the filename of the page that edits user information? Since the edit page is searched, the built-in ruby “edit.html.erb” is correct.

2.3 Microposts resource

■rails generate scaffold Micropost content:text user_id:integer Generate a migration file with id, content and user_id.

■ resources :microposts Supports microposts index create new edit show update destroy Are all created. “:” is a symbol. This will generate microposts_controller.rb.

[Practice] Please try all of 1, 2, 3 and 4.

■ validates :content, length: {maximum: 140} Validation to display an error when the content length exceeds 140 characters.

■ validates method Nuance that validate A,B displays error if A is not B.

[Practice] abridgement

■ has_many :microposts A user has multiple microposts. Described in model because it is involved in the database. Since it is an attribute related to the user, describe it in user.rb.

■ belongs_to :user A micropost belongs to only one user. Since it is related to the database, describe it in model. Since it is related to posting, describe it in micropost.rb.


  1. Let’s edit the user’s show page to show the user’s first micropost. Try guessing the grammar from the other code in the same file (it’s where the technology introduced in Box 1.1 comes into play). Let’s check /users/1 to see if it was displayed properly.

Since there are “show page” and “display”, it is a view in MVC. If you look in the view folder, there is show.html.erb, so let’s look inside. There is a description that shows the name and e-mail, so it is like this if you copy it.


<p id="notice"><%= notice %></p>

  <%= @user.name %>

  <%= @user.email %>

  <%= @user.microposts.first.content %>

<%= link_to'Edit', edit_user_path(@user) %> |
<%= link_to'Back', users_path %>

@user is an instance variable. It is declared in user_controller.rb. Here, @user = User.find(params[:id]) is specified. Call the post content with microposts and the first post with first. .content is unknown. Are you spoiled?

  1. Listing 2.16 is a validation that verifies the existence of a Micropost Content. Let’s try to see if we can verify that the micropost is not empty (it looks like Figure 2.16 is successful). Please describe it exactly.

  2. Try rewriting the FILL_IN in Listing 2.17 to verify that the User model name and email exist (Figure 2.17). → The real problem.


class User <ApplicationRecord
  has_many :microposts
  validates name, presence: true # Replace "FILL_IN" with your code
  validates email, presence: true # Replace "FILL_IN" with your code

■ Inheritance The model inherits ApplicationRecord. The controller inherits ApplicationController.

■ApplicationRecord, ApplicationController It’s like a package in which various things are already defined. It is unknown what is defined. I interpret it as something like a spell now.


  1. Open the Application controller file and find the code where ApplicationController extends ActionController::Base. → Code of the first line.

  2. Where’s the code where ApplicationRecord extends ActiveRecord::Base? Find it by referring to the previous exercise. (Hint: It works essentially the same as the controller, so check the files in the app/models directory…?) →There is a file called application_record.rb. I wasn’t aware of it until I was told. Here is the top code.

Please rinse the rest lightly.