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 --skip-assets flag.

So running 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!

Sajad Torkamani

Hello! My name is Sajad. I’m a software developer at Nationwide and an aspiring entrepreneur. Originally from Afghanistan, I moved to London at an early age and have been living here for the past 20 years.

This is my personal website where I blog mostly about programming with occasional thoughts on a bunch of other things. I write mainly for my own benefit as I find that writing helps clarify my otherwise very confused thoughts.

Get in touch: sajadtorkamani1@gmail.com