LoginSignup
4
1

More than 3 years have passed since last update.

scaffoldの使い方

Posted at

scaffoldとは

モデルやコントローラー、ビューを作っていき、さらに必要なルーティングを作成していく必要があるがrailsには簡単にアプリケーションの雛形を作ってくれる機能があり、それがscaffoldです。scaffoldを使うことで素早くrailsアプリケーションを作ることができる。

作成手順

rails new sample

作成後、該当ディレクトリへ移動する

cd sample

移動後に下記コマンドを実行

rails generate scaffold モデル名 カラム名1:データ型1 カラム名2:データ型 2 …
example

rails g scaffold user name:string age:integer

指定できる型一覧

  • string : 文字列
  • text : 長い文字列
  • integer : 整数
  • float : 浮動小数
  • decimal : 精度の高い小数
  • datetime : 日時
  • timestamp : タイムスタンプ
  • time : 時間
  • date : 日付
  • binary : バイナリデータ
  • boolean : Boolean

作成されるもの

  • マイグレーションファイル
    • db/migrate/xxxxxxxxx_create_users.rb
  • モデル
    • app/models/user.rb
  • コントローラー
    • app/controllers/users_controller.rb
  • ルーティング
    • config/routes.rb
  • ビュー
    • app/views/users/index.html.erb
    • app/views/users/edit.html.erb
    • app/views/users/show.html.erb
    • app/views/users/new.html.erb
    • app/views/users/_form.html.erb

routes.rbでは7つのアクションが自動的に定義される。

rake db:migrate

テーブルの作成

以上

4
1
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
4
1