DEV Community

Akshay Khot
Akshay Khot

Posted on • Originally published at akshaykhot.com

The Rails Configuration File (~/.railsrc)

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

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

The Rails Configuration File

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 made a switch 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 you do this.

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

touch ~/.railsrc
Enter fullscreen mode Exit fullscreen mode

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

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

--template=~/dotfiles/rails_template.rb
Enter fullscreen mode Exit fullscreen mode

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 

  ...
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

--template=~/software/rails/template.rb
Enter fullscreen mode Exit fullscreen mode

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?


This post was originally published on my blog at https://akshaykhot.com/railsrc-rails-configuration-file/

Top comments (0)