The Life‑Changing Magic of Ruby and Rails

The Rails Configuration File

Did you know that Rails has a ~/.railsrc file?

rails c
rails configuration file

Similar to your ~/.bashrc or ~/.zshrc file, you can use this file to configure your Rails applications.

This is especially useful if you find yourself repeatedly typing --skip or --no-skip commands and installing specific gems every time you create a new Rails application.

Since I switched to Rails from .NET last year, I must have created at least 20-30 projects for either learning, experiments, or client work. I have a text file that documents all the gems I want to install for a fresh Rails app. Every time I create a new project, I go through the file to install all the gems I want for my project.

With the ~/.railsrc file, I don’t need to do that. Rails will do it for me. Here’s how it works.

First, create the .railsrc file in your home directory.

touch ~/.railsrc

Add whatever options you want in this file. For example,

--database=mysql
--skip-active-job
--skip-spring
--skip-javascript

--template=~/dotfiles/rails_template.rb

To see all the available options, type rails in a non-rails directory.

➜  rails rails
Usage:
  rails new APP_PATH [options]

Options:
      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
  -r, [--ruby=PATH]                                          # Path to the Ruby binary of your choice
                                                             # Default: /Users/akshay/.rbenv/versions/3.1.0/bin/ruby
  -m, [--template=TEMPLATE]                                  # Path to some application template (can be a filesystem path or URL)
  -d, [--database=DATABASE]                                  # Preconfigure for selected database 
  
  ...

To pre-configure the gems you’d like to install, create a template.rb file. Here’s mine:

gem_group :development, :test do
  gem 'dotenv-rails'
  gem 'factory_bot_rails'
end

gem_group :development do
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'annotate'
end

Now add the path to the template at the end of your ~/.railsrc file.

--template=~/software/rails/template.rb

That’s it. The next time you run rails new app, Rails will use the configuration file along with the Gemfile template to create your application just like you want.

Pretty cool, right?

Subscribe to Akshay's Blog

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe