Chewing Rails Tutorial [Chapter 2 Toy Application]

2.1 Application planning

■rails generate There are four ways to use it. 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 everything (controller, view, model, migration file, routing).

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

■ Migration A mechanism to operate DB tables based on migration files. You can mess with the DB without writing SQL statements directly. (You can create a table)

■ Migration file A blueprint for generating a 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 The magic that makes Ruby faster.

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

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

[Practice]

    1. It is troublesome to make a figure, so pass.
  1. Look at the behavior shown and look for the code generated by Scaffold that gets the user information from the database. → The model is related to the database. However, the model created this time ・ Application_record.rb ・ User.rb There is no corresponding code in. Then, on the controller side, look for the part related to the model. There aren't many places where @user = ~ is written.

user_controller.rb


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

Create a method called set_user. I'm looking for a user in User.find. find () is a method to find a model. params is also a method. The contents can be called with params [: column name]. Here we are calling the id in the model.

    1. What is the filename of the page that edits the user's information? Since I'm looking for an edit page, the correct answer is "edit.html.erb" in the built-in ruby.

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 Corresponds to microposts index create new edit show update destroy Are all created. ":" Is a symbol. This will generate microposts_controller.rb.

[Practice] Try any of 1, 2, 3, or 4.

■validates :content, length: { maximum: 140 } Validation that displays an error when the length of the content exceeds 140 characters.

■ validates method validate A, B with a nuance that displays an error if A is not B.

[Practice] abridgement

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

■belongs_to :user One micropost belongs to only one user. Since it is related to the database, it is described in model. Since it is related to posting, it will be described in micropost.rb.

[Practice]

    1. Let's edit the user's show page to see the user's first micropost. Try guessing the grammar from the other code in the file (this is where the technique introduced in column 1.1 comes into play). Let's go to / users / 1 to see if it looks good.

Since it says "show page" and "display", it is a view in MVC. If you look for the view folder, you will find show.html.erb, so let's take a look inside. There is a description that the name and email are displayed, so it is like this if you imitate it.

ruby:show.html.erb


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

<p>
  <strong>Name:</strong>
  <%= @user.name %>
</p>

<p>
  <strong>Email:</strong>
  <%= @user.email %>
</p>

<p>
  <strong>Content:</strong>
  <%= @user.microposts.first.content %>
</p>

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

@user is an instance variable. Declared in user_controller.rb. Here, @user = User.find (params [: id]) is specified. Call the post content with microposts and the very first post with first. .content is unknown. Isn't it magic?

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

    1. Try rewriting the FILL_IN part in Listing 2.17 to verify that the User model name and email exist (Figure 2.17). → That's the problem.

ruby:show.html.erb


class User < ApplicationRecord
  has_many :microposts
  validates name, presence: true    # 「FILL_Replace "IN" with code
  validates email, presence: true    # 「FILL_Replace "IN" with code
end

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

■ApplicationRecord、ApplicationController It's like a package in which various things are already defined. It is unknown what is defined. Interpreted as something like magic now.

[Practice]

    1. Open the application controller file and look for the code where the application controller inherits from ActionController :: Base. → The code on the first line.
  1. Where's the code where ApplicationRecord inherits ActiveRecord :: Base? Use the exercises above to find out. Tip: It's essentially the same mechanism as the controller, so if you look at 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 flush the rest.

Recommended Posts

Chewing Rails Tutorial [Chapter 2 Toy Application]
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails Tutorial Chapter 4] Rails-flavored Ruby
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] Second Half
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] First Half
rails tutorial
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
Rails Tutorial (4th Edition) Memo Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
Rails tutorial test
Rails tutorial memorandum 1
Rails tutorial memorandum 2
Start Rails Tutorial
[Beginner] Rails Tutorial
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
Chapter 4 Rails Flavored Ruby
About rails application server
Rails Tutorial cheat sheet
[Rails] Learning with Rails tutorial
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)
[Rails] Create an application
rails tutorial fighting notes Ⅲ
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot
11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11