Micropostsリソース | rails チュートリアル 3

2.3 Micropostsリソースを勉強してみる。

generateとmigrate

# rails generate scaffold User name:string email:string
$ rails generate scaffold Micropost content:text user_id:integer
Running via Spring preloader in process 20989
      invoke  active_record
      create    db/migrate/20170629211108_create_microposts.rb
      create    app/models/micropost.rb
      invoke    test_unit
      create      test/models/micropost_test.rb
      create      test/fixtures/microposts.yml
      invoke  resource_route
       route    resources :microposts
      invoke  scaffold_controller
      create    app/controllers/microposts_controller.rb
      invoke    erb
      create      app/views/microposts
      create      app/views/microposts/index.html.erb
      create      app/views/microposts/edit.html.erb
      create      app/views/microposts/show.html.erb
      create      app/views/microposts/new.html.erb
      create      app/views/microposts/_form.html.erb
      invoke    test_unit
      create      test/controllers/microposts_controller_test.rb
      invoke    helper
      create      app/helpers/microposts_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/microposts/index.json.jbuilder
      create      app/views/microposts/show.json.jbuilder
      create      app/views/microposts/_micropost.json.jbuilder
      invoke  test_unit
      create    test/system/microposts_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/microposts.coffee
      invoke    scss
      create      app/assets/stylesheets/microposts.scss
      invoke  scss
    conflict    app/assets/stylesheets/scaffolds.scss
Cannot read termcap database;
using dumb terminal settings.
  Overwrite /Users/kkosuke/Works/house01/app/assets/stylesheets/scaffolds.scss? (enter "h" for help) [Ynaqdh] y
       force    app/assets/stylesheets/scaffolds.scss

$ rails db:migrate
== 20170629211108 CreateMicroposts: migrating =================================
-- create_table(:microposts)
   -> 0.0204s
== 20170629211108 CreateMicroposts: migrated (0.0205s) ========================

ルーティングを確認する

ここらで、routesを確認する

# routes.rb
Rails.application.routes.draw do
  resources :microposts
  resources :users
  root 'users#index'
end
# bash
$ rails routes
        Prefix Verb   URI Pattern                    Controller#Action
    microposts GET    /microposts(.:format)          microposts#index
               POST   /microposts(.:format)          microposts#create
 new_micropost GET    /microposts/new(.:format)      microposts#new
edit_micropost GET    /microposts/:id/edit(.:format) microposts#edit
     micropost GET    /microposts/:id(.:format)      microposts#show
               PATCH  /microposts/:id(.:format)      microposts#update
               PUT    /microposts/:id(.:format)      microposts#update
               DELETE /microposts/:id(.:format)      microposts#destroy
         users GET    /users(.:format)               users#index
               POST   /users(.:format)               users#create
      new_user GET    /users/new(.:format)           users#new
     edit_user GET    /users/:id/edit(.:format)      users#edit
          user GET    /users/:id(.:format)           users#show
               PATCH  /users/:id(.:format)           users#update
               PUT    /users/:id(.:format)           users#update
               DELETE /users/:id(.:format)           users#destroy
          root GET    /                              users#index

modelを修正する

micropost.rb

class Micropost < ApplicationRecord
  # userと紐付ける
  belongs_to :user
  # 未入力や、141文字以上を受け付けなくする。
  validates :content, length: { maximum: 140 }, presence: true
end

user.rb

class User < ApplicationRecord
  # micropostと紐付ける
  has_many :microposts
  # 未入力を受け付けなくする
  validates :name, presence: true
  validates :email, presence: true
end

参考

Ruby on Rails チュートリアル 第2章 - 演習&答え - Qiita

疑問

  • rails generateの使い方
  • scaffoldとは
  • has_many、belongs_toの使い方

つぎは、1.5 デプロイするあたりをやりたい。

コメント

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

内容に問題なければ、下記の「コメントを送信する」ボタンを押してください。

同じカテゴリの前後の記事


  1. KATOON.NET
  2. TRASH
  3. Micropostsリソース | rails チュートリアル 3