If you create a lot of Rails applications, you know that it can quickly become tedious to do things like:

  • disable/turn off Test Unit
  • Add RSpec and Cucumber
  • Add other gems such as Slim or HAML
  • Switch the default database engine to PostgreSQL

Luckily that are ways you can customize what the Rails application generator generates. In this article, we will look at a couple different methods that can be used to customize the default behavior of the Rails application generator. Let's get started.

Method One: Introducing .railsrc

The first method we will look at involves a little known file called .railsrc which you can create and place in your user's home directory. Inside the .railsrc file you can add arguments that tell Rails to do things like skip Test Unit or use PostgreSQL as the database engine. The .railsrc file takes the exact same arguments as the rails new command itself, so simply running rails new --help will provide you a list of all of the options. For example, adding the following line to the .railsrc file to skip Test Unit and add PostgreSQL as the default database engine.

.railsrc:

-d postgresql --skip-test-unit

This is great for more simplistic scenarios, but what if our needs are greater? For instance, what if we want to add certain gems to our gemfile or create a markdown version of the readme file? Fortunately this is pretty easy using Rails app templates.

Method Two: Rails Application Templates

Rails application templates take things a step further by allowing you to run actual commands during the generation process. For example, let's say we want to install rspec and slim, Create a readme.md file, and commit the whole thing to git. We can easily do this by creating an Rails application template. To test this, create a file called app_template.rb and add in the code listed below.

app_template.rb:

remove_file 'README.doc'
create_file 'README.md'

gem 'rspec-rails'
gem 'slim'
gem 'slim-rails'
run 'bundle install'
generate 'rspec:install'
git :init
git add: '--all', commit: '-m "Initial Commit" '

Now if we generate a new app with the parameters -m app_template.rb it will follow the instructions you listed in the template. For example, the code listed above will get executed when you run the following command.

Terminal Commands:

rails new blog -m app_template.rb

If you examine your project you'll see that first, rspec, slim, and slim-rails got installed, second, the default readme file was removed and a readme.md file was added, and finally, everything was committed to git.

In addition, you can have the application template prompt the user for more information. The code listed below allows you to do just that.

app_template.rb:

remove_file 'README.doc'
create_file 'README.md'

gem 'rspec-rails'
gem 'slim'
gem 'slim-rails'
run 'bundle install'
generate 'rspec:install'

if yes? 'Do you wish to generate a root controller? (y/n)'
  name = ask('What do you want to call it?').underscore 
  generate :controller, "#{name} show"
  route "root to: '#{name}\#show'"
  route "resource :#{name}, only: [:show]"
end

git :init
git add: '--all', commit: '-m "Initial Commit" '

In the code above, the yes? function will prompt the user with a yes/no question. The ask function will prompt the user for text input. In this case, we ask the user if they wish to generate a root controller, and if they say yes, we prompt them for a name.

Until Rails 4 was released, there was the even more powerful option of overriding the Rails AppBuilder itself, but unfortunately the release of Rails 4 removed this functionality. That's it. Thanks for reading!