22
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

初心者目線のrails generate 〜コマンドまとめ【結局どれでどのファイルができるんよ?!】

Last updated at Posted at 2023-04-19

初心者の俺 「rails generate 〜コマンドって…」

「結局どれでどのファイルができるんよ?!」

model views controller migration assets route test helper
scaffold
model
controller   
migration

ほぅ。
じゃあ、Userに関する機能追加をしたい場合実際のコマンドはどう書けばいいのよ??

scaffold

rails generate scaffold モデル名 [カラム名:型]

rails g scaffold User name:string email:string

model

rails generate scaffold モデル名 [カラム名:型]

rails g model User name:string email:string

俺へ

💢modelは単数形って何回言えばわかるのよ!!

俺より

controller

rails generate controller コントローラ名 [action action]

rails generate controller Users new create

俺へ

🙉コントローラは複数形だ!!!聞いてるの?!?!

俺より

migration

rails generate migration 名前 [カラム名:型[:index]]

rails generate migration AddUser name:string email:string

マイグレーション作ったら、rails db:migrateしないとエラー出るわよ。
migrationについてはこちら↓

「🙋‍♀️先生、間違えました」

なんだと?!あっ、やっぱり単数形にsnwjpjpiwfwjpiwipklfhihvie!!!!!!!!

scaffoldの削除

rails destroy scaffold User

関連ファイルも含めて削除できる。はず。

modelの削除

まずは現状のmigration状況を確認なさい

rails db:migrate:status 
rails db:migrateしている場合(migrationがup状態の場合)
rails db:rollback  #1つ前の$rails db:migrateを取り消すコマンド
rails db:migrate:status  #ちゃんとupからdownになっているか確認
modelを削除

rails destroy model モデル名

rails destroy model User

controllerを削除

rails destroy controller Users 

migrationを削除

rails destroy migration AddUser

migrationを好き勝手消すとえらいことになりますよ……詳しくはこちら↓

参考:実際のターミナルログ
 % rails g scaffold User name:string email:string
      invoke  active_record
      create    db/migrate/20230419030045_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      create      app/views/users/_user.html.erb
      invoke    resource_route
      invoke    test_unit
      create      test/controllers/users_controller_test.rb
      create      test/system/users_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/users/index.json.jbuilder
      create      app/views/users/show.json.jbuilder
      create      app/views/users/_user.json.jbuilder
% rails destroy scaffold User
      invoke  active_record
      remove    db/migrate/20230419030045_create_users.rb
      remove    app/models/user.rb
      invoke    test_unit
      remove      test/models/user_test.rb
      remove      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      remove    app/controllers/users_controller.rb
      invoke    erb
      remove      app/views/users
      remove      app/views/users/index.html.erb
      remove      app/views/users/edit.html.erb
      remove      app/views/users/show.html.erb
      remove      app/views/users/new.html.erb
      remove      app/views/users/_form.html.erb
      remove      app/views/users/_user.html.erb
      invoke    resource_route
      invoke    test_unit
      remove      test/controllers/users_controller_test.rb
      remove      test/system/users_test.rb
      invoke    helper
      remove      app/helpers/users_helper.rb
      invoke      test_unit
      invoke    jbuilder
      remove      app/views/users
      remove      app/views/users/index.json.jbuilder
      remove      app/views/users/show.json.jbuilder
      remove      app/views/users/_user.json.jbuilder
%  rails g model User name:string email:string
      invoke  active_record
      create    db/migrate/20230419030120_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
% rails destroy model User
      invoke  active_record
      remove    db/migrate/20230419030120_create_users.rb
      remove    app/models/user.rb
      invoke    test_unit
      remove      test/models/user_test.rb
      remove      test/fixtures/users.yml
% rails generate controller Users new create
      create  app/controllers/users_controller.rb
       route  get 'users/new'
              get 'users/create'
      invoke  erb
      create    app/views/users
      create    app/views/users/new.html.erb
      create    app/views/users/create.html.erb
      invoke  test_unit
      create    test/controllers/users_controller_test.rb
      invoke  helper
      create    app/helpers/users_helper.rb
      invoke    test_unit
% rails destroy controller Users    
      remove  app/controllers/users_controller.rb
      invoke  erb
      remove    app/views/users
      invoke  test_unit
      remove    test/controllers/users_controller_test.rb
      invoke  helper
      remove    app/helpers/users_helper.rb
      invoke    test_unit
% rails generate migration AddUser name:string email:string
      invoke  active_record
      create    db/migrate/20230419030255_add_user.rb
% rails destroy migration AddUser
      invoke  active_record
      remove    db/migrate/20230419030255_add_user.rb

参考にしたもの

Railsドキュメント>railsコマンド(rails)
いつも忘れる「Railsのgenerateコマンド」の備忘録

22
10
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
22
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?