Skip to the workspace
Pebellslearn by building
Meet Rails  ›  Lesson

The Rails Command Line and Generators

Rails Generators 12 minbin/rails runs every task in an app, and generators write the conventional files for a model, controller or whole resource so you start from correct names and locations.

ruby on rails / The Rails Command Line and Generators

Explore each scene at your own pace.

01 · The rule

The rule

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 suite

Generators 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 get routes.
  • 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).

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.