The Rails Config File
You must have used a ~/.bashrc or ~/.zshrc file to configure your environment. Did you know Ruby on Rails has a ~/.railsrc file that configures your Rails applications?
This is really useful if you find yourself using `--skip` or `--no-skip` flags 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 learning, experiments, and side projects. I had a text file that documented all the gems I wanted to install for a fresh Rails app. Every time I created a new project, I used to go through the file to install all the gems I wanted.
~/.railsrc file, you don’t need to do that. Rails will do it for you.
Here’s how it works.
First, create the
.railsrc file in your home directory.
Add whatever options you want in this file. For example,
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 ...
Install Gems Using a Template
To pre-configure the gems you’d like to install, create a
gem_group :development, :test do gem 'dotenv-rails' gem 'factory_bot_rails' end gem_group :development do gem 'annotate' gem 'better_errors' gem 'binding_of_caller' gem 'pry-byebug' end
Now add the path to the template at the end of your
~/.railsrc file, so the configuration file can use it.
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?
Let me know what you think in the comments below. If you already use a Rails config file, feel free to share your configuration!