Write Software, Well

January 11, 2024

The Rails Config File

Since I started learning Rails, I must have created dozens and dozens of Rails applications for learning, experiments, and side projects. I had a text file that documented all the gems I wanted to install for a fresh Rails app, along with the common app settings. Every time I created a new project, I used to go through the file to install all the gems I wanted.

I wish somebody had told me about the Rails config file.

With the ~/.railsrc file, you don’t need to do that. Rails will configure your applications for you. This is even more useful if you find yourself using --skip or --no-skip flags and installing specific gems every time you create a new Rails application.

Let's learn how it works.

Step 1: Create the Config File

First, create a text file named .railsrc in your home directory.

touch ~/.railsrc

Next, add whatever Rails-specific options you want in this file. For example,

--database=mysql
--css=tailwind
--skip-jbuilder

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

To see all the available options to configure the Rails app, type rails in a non-rails directory.

➜ 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

...

Choose whatever options you need to configure your future Rails applications.

Step 2: Install Gems Using a Template

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

gem 'lograge'

gem_group :development, :test do
gem 'awesome_print'
gem 'dotenv-rails'
gem 'factory_bot_rails'
gem 'minitest'
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, so the configuration file can use it.

--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.

For more information about the Rails application templates, check out this guide: Rails Application Templates — Ruby on Rails Guides

Pretty cool, right?

Let me know what you think in the comments below. If you already use a Rails config file, do share your configuration!

Subscribe to get future posts via email (or grab the RSS feed)

Comments (10)

J
Juzer Shakir

Hey Akshay, thanks for sharing with us! :) This line: --template=~/software/rails/template.rb I am curious what does `=~` mean?

A
Akshay Khot

Hey Juzer, the `~` means the home directory on your machine (Linux or Mac), stored on the $HOME variable. You can also provide the complete path instead. Hope that answers your question. If not, let me know.

R
Raj

Hi Akshay, I get to learn new productive hacks, techniques and things about Ruby/Rails from you articles. Thanks for the good work. Keep It Up. 👍🏻

A
Akshay Khot

Happy to hear that you're finding my posts helpful, Raj! Keep in touch.

A
Alexander Rusa

Wow, thanks! I also didn’t know that. Those templates have a lot more features. You can also use them to add an initializer or run an commands and rake tasks: https://guides.rubyonrails.org/rails_application_templates.html

A
Akshay Khot

Very cool! Thanks for sharing, Alexander ✌️ I didn't even know this guide existed.

E
Ed Ruder

Thanks for the post–I learned something! BTW, `amazing_print` is a fork of `awesome_print` that isn't stale–I suggest using it in your `template.rb` file.

A
Akshay Khot

Wow, didn't know that. Thanks for sharing, Ed! Glad we both learned something new :)

A
Aniket Patidar

Really enjoyed it ✨

A
Akshay Khot

Thank you for sharing 😊

Sign in to leave a comment.