The Rails Command Line and Generators
A Rails developer spends a surprising amount of time in the terminal: starting the server, running migrations, poking at data in a console, and generating files. One entry point covers all of it, and generators make sure new files are born with the right names in the right folders.
The rule
bin/rails runs the app's tasks; generate writes the boilerplate.
The everyday commands:
bin/rails server # start the app (short: bin/rails s)
bin/rails console # a Ruby prompt with your app loaded (bin/rails c)
bin/rails db:migrate # apply new migrations
bin/rails db:rollback # undo the last migration
bin/rails routes # list every route
bin/rails test # run the test suiteGenerators come in sizes:
bin/rails generate model Book title:string pages:integer
bin/rails generate controller Pages home about
bin/rails generate scaffold Book title:string pages:integer- model: the model class, a migration and tests.
- controller: the controller with the listed actions, a view for each and matching
getroutes. - scaffold: everything for a resource — model, migration, controller with all seven actions, views, routes and tests.
bin/rails g is short for generate, and bin/rails destroy model Book deletes what the generator made. Generators follow naming conventions for you: a model is singular (Book), its table and controller are plural (books, BooksController).