How to skip unwanted generators in Rails
Rails’s generator commands are awesome, allowing you to scaffold models, controllers, views, and a bunch of other things with a simple command like:
rails g controller articles
However, sometimes the ‘bunch of other things’ like helper files or sass stylesheets aren’t needed.
For example, assuming we’re using RSpec, running
rails g controller articles will create a bunch of files and Rails helpfully prints out the names of these files like so:
create app/controllers/articles_controller.rb invoke erb create app/views/articles invoke rspec create spec/requests/articles_request_spec.rb invoke helper create app/helpers/articles_helper.rb invoke rspec create spec/helpers/articles_helper_spec.rb invoke assets invoke scss create app/assets/stylesheets/articles.scss
We can tell Rails to skip the
helper generator by passing a skip flag like
--skip-helper or skip the
assets generator with the
rails g controller articles --skip-helper --skip-assets would skip the helper and scss files (things I usually want to skip) and result in fewer generated files:
create app/controllers/articles_controller.rb invoke erb create app/views/articles invoke rspec create spec/requests/articles_request_spec.rb
Just another reason Rails is awesome!