Rails Tutorial Chapter 2 Learning

Where I thought (.´ ・ ω ・) in Chapter 2 of Rails Tutorial

Exercises For readers familiar with CSS: Create a new user and use your browser's HTML Inspector feature to look up "User was successfully created." What happens when I reload the browser?

Google Chrome Shift + Ctrl + I or (F12) or right-click and click verification image.png You can see the HTML / CSS code, I think you should use this function to check "User was successfully created." ('Ω') ノ If you want to know a little more, please go to here.

A brief review of the content.

Create a user registration function Magic command "Rails g scaffold" (scaffold means scaffolding in Japanese)

$ rails generate scaffold User name:string email:string
$ rails db:migrate

Just hit these two commands and you'll have a page like this. User registration is possible ... It's already a magical area. image.png

Disadvantages of Scaffold

  • There is no data validation. (You can register even with a blank or random address)
  • There is no user authentication. (There is no login / logout function. Anyone can access the user page)
  • The layout and style are not in order.
  • No test written.
  • Difficult to understand.

Create a posting function

$ rails generate scaffold Micropost content:text user_id:integer
$ rails db:migrate

You can't just hit two commands as before.

  • It is necessary to add routing.

ruby config/routes.rb


Rails.application.routes.draw do
  resources :microposts #← Add this to url/You can access the post page with micropost
  resources :users
  root 'users#index'
end

Verification function can be implemented by adding validates to models / micropost.rb

ruby app/models/micropost.rb


class Micropost < ApplicationRecord
  validates :name, length: { maximum: 140 } #You can enter up to 140 characters
                   presence: true #Confirm that the character string is entered
end

Associate different data models with each other

ruby app/models/user.rb


class User < ApplicationRecord
#Users have a lot of microposts so has many:Add micropost
  has_many :microposts
end

ruby app/models/micropost.rb


class Micropost < ApplicationRecord
#Since the micropost is tied to the user, belongs_to :add user
  belongs_to :user
  validates :content, length: { maximum: 140 }
end

This completes the RDB association. Wow easy (* ´ 艸 `)

Rails Console (a convenient tool that allows you to interact with Rails apps)

#Rails console
$ rails c
>> #If you write Ruby code here, it will be executed

>> first_user = User.first #Variable first for user with id 1 from User table_Store in user

Command processing content
User.first
↓ (It is automatically converted into a SQL command that operates the database.)
User Load (0.2ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC
  LIMIT ?  [["LIMIT", 1]]
↓ SQL execution result
 => #<User id: 1, name: "Michael Hartl", email: "[email protected]",
 created_at: "2020-10-23 00:39:14", updated_at: "2020-10-23 00:41:24">
↓ 
SQL execution result first_Store in user

>> first_user.microposts #Take out all the microposts posted by the user earlier.
Micropost Load (3.2ms)  SELECT "microposts".* FROM "microposts" WHERE
  "microposts"."user_id" = ? LIMIT ?  [["user_id", 1], ["LIMIT", 11]]
 => #<ActiveRecord::Associations::CollectionProxy [#<Micropost id: 1, content:
 "First micropost!", user_id: 1, created_at: "2020-10-23 02:04:13", updated_at:
 "2020-10-23 02:04:13">, #<Micropost id: 2, content: "Second micropost",
 user_id: 1, created_at: "2020-10-23 02:04:30", updated_at: "2020-10-23
 02:04:30">]>

>> micropost = first_user.microposts.first #first_Store user's first post in variable micropost

Impressions

This chapter didn't really stumble. ('ω') ノ I have fallen in love with the convenience of git & heroku.

Recommended Posts