【Ruby on Rails】rails g scaffoldとは
scaffoldとは「足場」の意味。railsでは基本的なDBに必要な物をまとめて作成してくれる。
サーバーを立ち上げると、DBの一覧を表示するページや、データの追加、編集、削除機能を含んだサイトができている。
目次
コマンド
rails g scaffold モデル名 カラム名1:データ型1 カラム名2:データ型2,,,,
 ┗ 「g」はgenerateの略
 ┗ データ型を指定しない場合はstringになる
$ rails g scaffold games name players
- games: モデル名
- name: カラム
- players: カラム
▼ターミナルの処理内容
      invoke  active_record
      create    db/migrate/20200825114158_create_games.rb
      create    app/models/game.rb
      invoke    test_unit
      create      test/models/game_test.rb
      create      test/fixtures/games.yml
      invoke  resource_route
       route    resources :games
      invoke  scaffold_controller
      create    app/controllers/games_controller.rb
      invoke    erb
      create      app/views/games
      create      app/views/games/index.html.erb
      create      app/views/games/edit.html.erb
      create      app/views/games/show.html.erb
      create      app/views/games/new.html.erb
      create      app/views/games/_form.html.erb
      invoke    test_unit
      create      test/controllers/games_controller_test.rb
      invoke    helper
      create      app/helpers/games_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/games/index.json.jbuilder
      create      app/views/games/show.json.jbuilder
      create      app/views/games/_game.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/games.coffee
      invoke    scss
      create      app/assets/stylesheets/games.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss
dbフォルダのmigrateや、models,views, controllers, assetsなど主要フォルダ内に新しいファイルが作成されている。
作成される主なファイル
1. controllersフォルダ
controllersフォルダの中に、モデル名_controller.rbが作成される。
DB操作に必要な7つのアクション、index, show, new, edit, create, update, destroyと各処理が記載されている。
### 2. modelsフォルダ modelsフォルダの中の、concernsフォルダの中に、model名.rbが作成される。
class Game < ApplicationRecord
end
処理は未記入。必要に応じてvalidateなどの処理を記述する。
class User < ApplicationRecord
    validates :name, 
               presence: {message: "名前を入力してください。"}
end
3. viewsフォルダ
viewsフォルダ内の、モデル名のフォルダの中に、各アクション名のついたテンプレートが生成されている。
各テンプレートは記述済み。DBの一覧表示や編集、詳細ページなどとなっている。
|  | 
|---|
4. dbフォルダ
dbフォルダの中のmigrateフォルダの中に 日時_create_モデル名.rbが作成されている
class CreateGames < ActiveRecord::Migration[5.0]
  def change
    create_table :games do |t|
      t.string :name
      t.string :players
      t.timestamps
    end
  end
end
DBとrailsを紐づける設計書(migration)が作成されている。
5. assetsのjavascriptフォルダ
モデル名.coffeeファイルが作成されている。
6. assetsのcssフォルダ
モデル名.scssと、scaffolds.scssの2つのファイルが作成されている。
7. localフォルダ
localフォルダのroutes.rbファイルにresources :gamesが追記されている。
Rails.application.routes.draw do
  resources :games
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
▼処理の中身をみるには、 `$ rake routes`を実行することで処理の中身を確認することができる。
user1:~/environment/boardgame-app $ rake routes
   Prefix Verb   URI Pattern               Controller#Action
    games GET    /games(.:format)          games#index
          POST   /games(.:format)          games#create
 new_game GET    /games/new(.:format)      games#new
edit_game GET    /games/:id/edit(.:format) games#edit
     game GET    /games/:id(.:format)      games#show
          PATCH  /games/:id(.:format)      games#update
          PUT    /games/:id(.:format)      games#update
          DELETE /games/:id(.:format)      games#destroy
### DB作成後の処理 ## migrationの実行 scaffoldで自動生成された、マイグレーションファイルを利用して、DBにテーブルを作成する。
$ rake db:migrate
サーバー立ち上げ
dbにテーブルの作成が完成したら、サーバーを立ち上げる。
$ rails s 
### 参考 >[Rails公式 migrateについて](https://railsguides.jp/active_record_migrations.html)





