Ruby and Rails

Ruby と Ruby on Rails のこと

rails generate

rails generaterails g と短縮可能。

dry run

rails g-p ( --pretend ) を付けることで dry run 可能。

実際にファイルは作らないので、複数形だっけ?なんのファイル作るんだっけ?という時の確認に便利。

$  rails g controller Dashboard -p
      create  app/controllers/dashboard_controller.rb
      invoke  erb
      create    app/views/dashboard
      invoke  test_unit
      create    test/controllers/dashboard_controller_test.rb
      invoke  helper
      create    app/helpers/dashboard_helper.rb
      invoke    test_unit
      create      test/helpers/dashboard_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/dashboard.js.coffee
      invoke    scss
      create      app/assets/stylesheets/dashboard.css.scss

コントローラの作成

コントローラ名は基本は複数形。Dashboard のように単数形でも可。

コントローラ名を ProductCategories と CamelCase で指定すると product_categories_controller.rb になる。

$ rails g controller ProductCategories
      create  app/controllers/product_categories_controller.rb
      invoke  erb
      create    app/views/product_categories
      invoke  test_unit
      create    test/controllers/product_categories_controller_test.rb
      invoke  helper
      create    app/helpers/product_categories_helper.rb
      invoke    test_unit
      create      test/helpers/product_categories_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/product_categories.js.coffee
      invoke    scss
      create      app/assets/stylesheets/product_categories.css.scss

ビューも指定可能。

$ rails g controller Dashboard index edit show new
      create  app/controllers/dashboard_controller.rb
       route  get 'dashboard/new'
       route  get 'dashboard/show'
       route  get 'dashboard/edit'
       route  get 'dashboard/index'
      invoke  erb
      create    app/views/dashboard
      create    app/views/dashboard/index.html.erb
      create    app/views/dashboard/edit.html.erb
      create    app/views/dashboard/show.html.erb
      create    app/views/dashboard/new.html.erb
      invoke  test_unit
      create    test/controllers/dashboard_controller_test.rb
      invoke  helper
      create    app/helpers/dashboard_helper.rb
      invoke    test_unit
      create      test/helpers/dashboard_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/dashboard.js.coffee
      invoke    scss
      create      app/assets/stylesheets/dashboard.css.scss

モデルの作成

モデル名は単数形。

id, created_at, updated_at カラムは自動で作られる。

$ rails g model Product name:string quantity:integer product_category:references
      invoke  active_record
      create    db/migrate/20141203093915_create_products.rb
      create    app/models/product.rb
      invoke    test_unit
      create      test/models/product_test.rb
      create      test/fixtures/products.yml

カラムに指定できる型は下記。

  • binary
  • boolean
  • date
  • datetime
  • decimal
  • float
  • integer
  • primary_key
  • references
  • string
  • text
  • time
  • timestamp

references は関連を表す。

ProductCategory と Product があった場合に product_category:references すると、product.product_category_id カラムを作成し、INDEX も張ってくれる。

Scaffold の作成

コントローラもモデルもまとめて生成。/product_categories/ にアクセスすれば CRUD 機能も用意されている。

モデル名を指定する。

$ rails g scaffold ProductCategory name:string
      invoke  active_record
      create    db/migrate/20141203093952_create_product_categories.rb
      create    app/models/product_category.rb
      invoke    test_unit
      create      test/models/product_category_test.rb
      create      test/fixtures/product_categories.yml
      invoke  resource_route
       route    resources :product_categories
      invoke  scaffold_controller
      create    app/controllers/product_categories_controller.rb
      invoke    erb
      create      app/views/product_categories
      create      app/views/product_categories/index.html.erb
      create      app/views/product_categories/edit.html.erb
      create      app/views/product_categories/show.html.erb
      create      app/views/product_categories/new.html.erb
      create      app/views/product_categories/_form.html.erb
      invoke    test_unit
      create      test/controllers/product_categories_controller_test.rb
      invoke    helper
      create      app/helpers/product_categories_helper.rb
      invoke      test_unit
      create        test/helpers/product_categories_helper_test.rb
      invoke    jbuilder
      create      app/views/product_categories/index.json.jbuilder
      create      app/views/product_categories/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/product_categories.js.coffee
      invoke    scss
      create      app/assets/stylesheets/product_categories.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

rails g の取り消し

間違えて rails g したファイルを rails destroy で一括削除できる。rails d と短縮可能。

$ rails g controller ProductCategories
$ rails d controller ProductCategories
$ rails g model Product name:string quantity:integer
$ rails d model Product
$ rails g scaffold ProductCategory name:string
$ rails d scaffold ProductCategory