LoginSignup
0
0

More than 3 years have passed since last update.

【Ruby on Rails】rails g scaffoldとは?DBの作成方法

Last updated at Posted at 2020-08-25

【Ruby on Rails】rails g scaffoldとは

scaffoldとは「足場」の意味。railsでは基本的なDBに必要な物をまとめて作成してくれる。

サーバーを立ち上げると、DBの一覧を表示するページや、データの追加、編集、削除機能を含んだサイトができている。

目次

  1. コマンド
  2. 作成される主なファイル
    1. controllersフォルダ
    2. modelsフォルダ
    3. viewsフォルダ
    4. dbフォルダ
    5. assetsのjavascriptフォルダ
    6. assetsのcssフォルダ
    7. localフォルダ
  3. DB作成後の処理
    1. migrationの実行
    2. サーバー立ち上げ

コマンド

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が作成される。

image.png

DB操作に必要な7つのアクション、index, show, new, edit, create, update, destroyと各処理が記載されている。


2. modelsフォルダ

modelsフォルダの中の、concernsフォルダの中に、model名.rbが作成される。

image.png

class Game < ApplicationRecord
end

処理は未記入。必要に応じてvalidateなどの処理を記述する。

modelsのファイル記述例
class User < ApplicationRecord
    validates :name, 
               presence: {message: "名前を入力してください。"}
end

3. viewsフォルダ

viewsフォルダ内の、モデル名のフォルダの中に、各アクション名のついたテンプレートが生成されている。

image.png

各テンプレートは記述済み。DBの一覧表示や編集、詳細ページなどとなっている。

image.png

4. dbフォルダ

dbフォルダの中のmigrateフォルダの中に 日時createモデル名.rbが作成されている

image.png

migrate
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ファイルが作成されている。

image.png

6. assetsのcssフォルダ

モデル名.scssと、scaffolds.scssの2つのファイルが作成されている。

image.png

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について

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0