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
g
instead of
generate
here.
rails g migration
This generator will just create a migration file under
db/migrate
as named with a timestamp. For example,
rails g migration CreateUsers
will generate a file called
20150717155949_create_users.rb
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:
app assets javascripts/static.coffee stylesheets/static.scss controllers/static_controller.rb helpers/static_helper.rb views/static/index.html.erb test controllers/static_controller_test.rb
as well as routes for your defined actions in your
config/routes.rb
file.
rails g scaffold
This is the big kahuna. The scaffold creates a model, controller, views, assets (styles and javascript), tests, and routes for the designated resource, as well as a database migration for the new table. The controller files are fully operable, complete with
index
,
create
,
new
,
edit
,
show
,
update
,
destroy
methods. Controller test files are also full of working tests.
rails g scaffold Post title body:text
will create the following files:
app assets javascripts/posts.coffee stylesheets/posts.scss controllers/posts_controller.rb helpers/posts_helper.rb models/post.rb views/posts _form.html.erb index.html.erb index.json.jbuilder edit.html.erb show.html.erb show.json.jbuilder new.html.erb test controllers/post_controller_test.rb fixtures/posts.yml models/post_test.rb
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
resources :posts
line in your
config/routes.rb
file,
rails g resource Purchase user_id:integer
will create:
app assets javascripts/posts.coffee stylesheets/posts.scss controllers/posts_controller.rb helpers/posts_helper.rb models/post.rb views/posts test controllers/post_controller_test.rb fixtures/posts.yml models/post_test.rb
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
will create:
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
will create:
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
will create:
app/helpers/auth_helper.rb
rails g task
Similarly to the helper generator, the task generator simply creates the named rake task file.
rails g task Cleanup
creates:
lib/tasks/cleanup.rake
rails g job
The job generator,
rails g job Import
for example, creates:
app/jobs/import_job.rb test/jobs/import_job_test.rb
rails g assets
If front-end's your thing, the assets generator creates only a javascript file and a css file as named.
rails g assets Smile
will create:
app/assets javascripts/smile.coffee stylesheets/smile.scss
rails g channel
New in Rails 5, this generator creates the necessary files for a new ActionCable channel.
rails g channel Chat speak
will create:
app assets/javascripts/channels/chat.coffee channels/chat_channel.rb
As of the 5.0 release,
app/assets/javascripts/channels/chat.coffee
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
and
app/channels/chat_channel.rb
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
rails d
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.