Jason Charnes

Disabling Rails Generators

August 4, 2021 ∙ Jason Charnes

I love spinning up a Rails controller using rails g controller. I don’t love all the files it brings with it. I end up with a treasure trove of files I either have to manually delete or commit into my git history for all eternity.

For example, I typically use Webpacker for all my assets. When I run rails g controller I don’t want autogenerated stylesheets in app/assets/. I want to disable assert generation.

If I’m going to use a helper I’d rather create it when I need it. I want to disable helper generation.

Whenever I spin up a new Rails app, I look at previous projects or search the web for how to disable certain generators. But no more, this will be my resource for it moving forward.

Consider this my love letter to the Ruby on Rails config.

This example is derived from the Rails guides. (I updated some of the comments in the docs for brevity.)

Inside config/application.rb:

module YourAmazingRailsApp
  class Application < Rails::Application
    # ... some stuff!

    # The commented out values are using the defaults.
    # To disable a generator (that is a boolean) change it from true to false.
    # Some generators take a symbol as a value, refer to the guides for the other options.
    #
    # Feel free to copy and paste this entire block in!
    config.generators do |generate|
      # generate.assets true # create assets when generating a scaffold
      # generate.force_plural false # allow pluralized model names
      # generate.helper true # generate helpers
      # generate.integration_tool :test_unit # which tool generates integration tests (might be overwritten automatically if using rspec-rails)
      # generate.system_tests :test_unit # which tool generates system tests (might be overwritten automatically if using rspec-rails)
      # generate.orm false # which orm to use (false uses Active Record)
      # generate.resource_controller :controller # which generator generates a controller when using bin/rails generate resource
      # generate.resource_route true # generate a resource route definition
      # generate.scaffold_controller :scaffold_controller # which generator generates a controller when using bin/rails generate scaffold
      # generate.stylesheets true # generate stylesheets
      # generate.stylesheet_engine :css # configures the stylesheet engine (for e.g. sass) to be used when generating assets. Defaults to :css.
      # generate.scaffold_stylesheet true # creates scaffold.css when generating a scaffolded resource. Defaults to true.
      # generate.test_framework false # which test framework to use (false uses Minitest) (might be overwritten automatically if using rspec-rails)
      # generate.template_engine :erb # which template engine to use
    end
  end
end

My typical config looks like this:

module YourAmazingRailsApp
  class Application < Rails::Application
    # ... some stuff!

    config.generators do |generate|
      generate.assets false # create assets when generating a scaffold
      generate.helper false # generate helpers
      generate.stylesheets false # generate stylesheets
    end
  end
end

To learn more, visit the Rails guides.