WideFix tech post

Don't generate trash with rails generators

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

Configure your application

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

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

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

Also I recommend to don’t pass action names in rails controller generator because it inserts automatically routes which always are changed later.

Say we have runned 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 which are never remained in this look. For my opinion it’s more useful to have pending tests than tests which 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

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


Thank you for your attention!

Discover Exclusive Ruby/Rails Development Opportunities! Submit Your Details to Receive Timely Job Notifications.

WideFix and Ruby On Rails expertise

At WideFix, we take pride in our expertise and experience. You can trust that our solutions will not harm your business and will always keep your site running smoothly. You can rely on us to provide confident and effective solutions that meet your needs.

Read also