Rails Generators Cheat Sheet

As a beginner of Rails, I often get confused about what generator generates what and the syntax of these commands. Below is a cheat sheet on the four main generators I find most useful.

What are they?

They build out standard features of certain parts of your rails application. To start any generator type rails g or rails generator followed by your command.

To find a list of all generators you can also type rails g. *make sure you are in your rails app*

You will see a terminal output that resembles below. This article will focus on the bolded generators: controller, migration, model, and resource.

General options:
-h, [--help] # Print generator's options and usage
-p, [--pretend] # Run but do not make any changes
-f, [--force] # Overwrite files that already exist
-s, [--skip] # Skip files that already exist
-q, [--quiet] # Suppress status output
Please choose a generator below.Rails:
application_record
assets
channel
controller
generator
helper
integration_test
jbuilder
job
mailbox
mailer
migration
model
resource
scaffold
scaffold_controller
system_test
task
ActiveRecord:
active_record:application_record
TestUnit:
test_unit:channel
test_unit:generator
test_unit:mailbox
test_unit:plugin

There are a lot more as you can see and you can easily learn about them by entering rails g <name of generator> in the terminal. Your terminal will output a short description of that generator.

Things to note

A lifesaver for me is that you can easily erase all traces of the generator you created by simply replacing the g in rails g <generator name> to a d like so, rails d <name of generator>. So don’t be afraid!

Also, note I added —-note-test-framework in the below examples, this does not create test files. If you need test files simply omit this command.

Lastly, since summer is almost over and who doesn’t like ice cream?! I chose ice cream as my generator example. Eat up!

Migration

The migration generator makes it easy to create and edit tables. To create a migration table follow below. You can also add columns to your command. String is the default datatype, so flavor in the example below is the same as size:string.

rails g migration create_ice_creams flavor size:string scoops:integer

You will see the below output telling you your table was created!

Running via Spring preloader in process 57527
invoke active_record
create db/migrate/20200906130653_create_ice_creams.rb

If you go into your db/migrate folder you will see the table set up! Now all you need to do is rails db:migrate. Sweet!

Model

To create a model generator type the following in your terminal. Just like the migration generator, you can include attributes.

rails g model IceCream flavor size scoops:integer --no-test-framework

The below output tells us our migration table was created with the attributes along with our model file!

Running via Spring preloader in process 77288
invoke active_record
create db/migrate/20200908121113_create_ice_creams.rb
create app/models/ice_cream.rb

Controller

For the controller generator instead of adding attributes like the model, you can add controller actions.

rails g controller ice_creams index show --no-test-framework

Your output will show that you just created quite a few files and folders!

Running via Spring preloader in process 58112
create app/controllers/ice_creams_controller.rb
route get 'ice_creams/index'get 'ice_creams/show'
invoke erb
create app/views/ice_creams
create app/views/ice_creams/index.html.erb
create app/views/ice_creams/show.html.erb
invoke helper
create app/helpers/ice_creams_helper.rb
invoke assets
invoke scss
create app/assets/stylesheets/ice_creams.scss

Let’s see what we created!

  • Our ice_creams_controller.rb with our index and show actions defined
  • Routes for index and show
  • A folder in the views folder called ice_creams with index and show html.erb files
  • helper files
  • stylesheets for the controller

Resource

This is the most powerful out of the four! You can also include attributes for your model.

rails g resource IceCream flavor size scoops:integer --no-test-framework

Your terminal output will look something like below

Running via Spring preloader in process 61311
invoke active_record
create db/migrate/20200906152644_create_ice_creams.rb
create app/models/ice_cream.rb
invoke controller
create app/controllers/ice_creams_controller.rb
invoke erb
create app/views/ice_creams
invoke helper
create app/helpers/ice_creams_helper.rb
invoke assets
invoke scss
create app/assets/stylesheets/ice_creams.scss
invoke resource_route
route resources :ice_creams

Here we created…

  • Migration table along with columns that we put as our attributes
  • The model
  • Controller with no actions
  • Ice_creams folder in views (note: no action html.erb files)
  • Helper files
  • Stylesheets for the controller
  • Resource Route for all CRUD actions

As you can see rails generators are very powerful and can save a lot of time if you know how to utilize them. Hope this helped and happy coding!

Former fashion designer turned software engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store