Find an article

Start typing to search the archive.

← All articles

Don't generate trash with Rails generators

When you generate a controller in a Rails application, many unexpected files are generated with it. I like to disable this behavior right after generating an application. This post describes how to configure Rails to avoid generating trash.

Let’s assume that we have a Rails >= 3 application. When you generate a controller, a lot of trash appears in your application. I mean these things: helpers, assets (CSS and JS) and view tests. In 99% of cases, these files are waste and never used, so it’s reasonable to disable their generation. It’s very easy.

Configure your application

Go to config/application.rb and paste these lines there:

config.generators do |g|
  g.assets = false
  g.helper = false
  g.view_specs = false
end

These magic lines of code will get rid of the headache, and you will forget about removing this trash manually forever!

I also recommend not passing action names to the Rails controller generator because it automatically inserts routes that always change later.

Say we have run the command rails g controller users new edit. It will generate the following trash.

Example of generated routes:

get "users/new"
get "users/edit"

It also generates tests that never remain in this form. In my opinion, it’s more useful to have pending tests than tests that do nothing.

Example of generated tests for controller:

require 'spec_helper'

describe LessonsController do

  describe "GET 'new'" do
    it "returns http success" do
      get 'new'
      response.should be_success
    end
  end

  describe "GET 'edit'" do
    it "returns http success" do
      get 'edit'
      response.should be_success
    end
  end

end

Thank you for your attention!