Customize Rails Generators to Avoid Generating Unnecessary Files

1 minute read

As you may already know when you a generate a new resource, model or controller there are bunch of other files that are also being generated for you. Rails has a great way to customize the default generators as you wish. For example let’s assume that we are creating an API for a mobile app. Let call this app sample_api Let’s create a controller for our sample_api

rails g controller posts

Rails Generator 1

And you can see the files that are generated for you. Well since this is an API, We don’t need any assets (Javascript and CSS). Normally we delete that folders but when we generate another resource we need to do the same again. Instead let’s customize our generator to disable those for us. In our config/application.rb

config.generators do |g|
  g.stylesheets     false
  g.javascripts     false

Now let’s see the generator in action again.

Rails Generator 2

Voila :) As you can see assets are no longer being generated for us. Customizing the generators can really improve our workflow. You can disable unwanted test or spec generation, disable assets or whatever you want. Hell, you can even create your own generators. Happy Hacking <3

Leave a Comment