July 17, 2016
Generators are one of Rails' greatest features and one of the reasons it's so easy to get up and running with new Rails projects. Unfortunately, they tend to be accompanied by a lot of extra files you may or may not need. Whenever I'm using generators, I like to know exactly what I'm getting into file-wise, ideally via a reference that lists what files I can expect to have when running them. Yes, this exists in the form of man pages for each individual generator, but I prefer to be able to see many at once, comparing my options. I've yet to see this kind of resource. Until now.
For this guide, I'll be using a Rails 5.0 default application with no additional gems and no modifications to the generator system. If you're looking to modify the content created by the generators, Thoughtbot has a helpful blog post on that here. Railsguides.net also has a great post on generator usage and getting more out of the basic generators here. With that said...
Note: the commands for running rails generators are
rails generate [type],
rails g [type]
for ease. I'll just be using
rails g migration
This generator will just create a migration file under
as named with a timestamp. For example,
rails g migration CreateUsers
will generate a file called
and it will contain the following text:
class CreateUsers < ActiveRecord::Migration[5.0] def change create_table :users do |t| end end end
You can also pass arguments to this generator for whatever arguments you're creating/modifying, like so:
rails g migration AddBirthdateToUsers birthdate:date
rails g model
The model generator creates a model file for the named resource, a database migration to create the appropriate table, and the appropriate test files. So, new files for
rails g model User
are as follows:
app/models/user.rb test models/user_test.rb fixtures/users.yml
rails g controller
The controller generator creates a controller, a controller test file, and an associated helper. You can also pass arguments in the form of controller actions to this generator. So,
rails g controller Static index
will create the following:
as well as routes for your defined actions in your
rails g scaffold
methods. Controller test files are also full of working tests.
rails g scaffold Post title body:text
will create the following files:
Note: adding an attribute argument with no defined datatype defaults to the string type.
rails g resource
Unlike the scaffold generator, this generator doesn't create views or add methods to the generated controller. In addition to the new table's database migration and a
line in your
rails g resource Purchase user_id:integer
rails g scaffold_controller
If you like the guidance of the scaffolded controllers and front-end files, but don't need any of the data modeling functionality, this generator is for you. Like the scaffold, the controller test file will be fully populated.
rails g scaffold_controller Hello
app controllers/hellos_controller.rb helpers/hellos_helper.rb views/hellos index.html.erb index.json.jbuilder edit.html.erb show.html.erb show.json.jbuilder new.html.erb _form.html.erb test/controllers/hellos_controller_test.rb
rails g mailer
This generator creates a mailer and blank mailer test files, named appropriately.
rails g mailer Campaign
app mailers/campaign_mailer.rb views/campaign_mailer test/mailers previews/campaign_mailer_preview.rb campaign_mailer_test.rb
rails g helper
This simply creates a helper file, again named as you please.
rails g helper Auth
rails g task
Similarly to the helper generator, the task generator simply creates the named rake task file.
rails g task Cleanup
rails g job
The job generator,
rails g job Import
for example, creates:
rails g assets
rails g assets Smile
rails g channel
New in Rails 5, this generator creates the necessary files for a new ActionCable channel.
rails g channel Chat speak
As of the 5.0 release,
will look like this:
App.chat = App.cable.subscriptions.create "ChatChannel", connected: -> # Called when the subscription is ready for use on the server disconnected: -> # Called when the subscription has been terminated by the server received: (data) -> # Called when there's incoming data on the websocket for this channel
will look like this:
class ChatChannel < ApplicationCable::Channel def subscribed # stream_from "some_channel" end def unsubscribed # Any cleanup needed when channel is unsubscribed end end
Finally, this acts as an undo button when you run a generator. Just as you'd run
rails g model User,
you can run
rails d model User
to delete all files generated with the model generator. You can use this with any of the generators listed above. Super useful if you find you've made a typo somewhere in your naming, or realize you don't really need all those files when you scaffold.
I've personally wanted a reference for these generators anytime I use them, so I hope this guide was as helpful for you as it is for me.