■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.
■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]
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.
■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]
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?
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.
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]
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