Blog

Rails Configuration Files

Placeholder Avatar
Rachelle LeQuesne
February 17, 2017

Note: This post is intended as a supplement to WTF Just Happened? A Quick Tour of Your First Rails App.

Important Configuration Files

People often say that Rails favours convention over configuration. However, that doesn’t mean you can forget about configuration altogether.

You may have noticed that your new Rails project comes with a config directory that contains a lot of files. So much for convention over configuration! Many of these files you don’t need to worry about, but there are times when you might need to make a few changes to customise the behaviour of your application.

Here we will discuss the main files, their purpose, and some customisations you might wish to make.

Starting Your Application

There are three main files involved in starting up your Rails application:

  • config/boot.rb Sets up Bundler and the load paths.

  • config/application.rb This contains any application-wide configuration settings for Rails and its components. e.g. Time Zone or Localisation

  • config/environment.rb This loads your Rails application and runs all the initializers

Initializers

The initializers directory contains all the configuration settings that need to be loaded after the framework and the gems are loaded. There are ten initializer files that are included in all Rails applications but you can add your own as required. It is considered best practise to group related initializers together into their own separate - and appropriately named - initializer file, rather than jam them all together into one large general file.

The ten initializer files that Rails provides for you are:

  • Application Controller Renderer Allows you to render arbitrary templates without having to be in a controller action.

  • Assets Allows you to specify additional stylesheets of JavaScript files to include.

  • Backtrace Silencers Allows you to modify the way that backtraces are shortened.

  • Cookies Serialiser Allows you to specify the way cookies are serialised.

  • Filter Parameter Logging Allows you to specify which parameters to filter out of your log files.

  • Inflections Contains the default inflections for pluralisation and singularisation of uncountable words.

  • Custom MIME Types Rails supports a standard set of MIME types. Adding to this file allows you to specify additional MIME types for your application to respond to.

  • New Framework Defaults Contains migration options to make upgrading Rails versions a little easier.

  • Session Store Allows you to specify what class to use to store the session.

  • Wrap Parameters Configures your application to work with JavaScript MVC frameworks, such as Backbone.js, out of the box.

Operation Environments

Every Rails application comes pre-configured with three different environments for operation: development, test, and production. Each environment has its own configuration file which specifies how things should work in that environment. These files can be found in the config/environments directory and are called, development.rb, test.rb, and production.rb. They include details such as which database to connect to, and whether classes in your application should be reloaded with each request. We use an environment variable called RAILS_ENV to determine which environment we are in.

Other Useful Files

** config/database.yml**: contains all the settings required by Active Record to connect to a database.

config/secrets.yml: stores your application’s sensitive data, such as access keys and passwords that are required for external APIs.

config/routes.rb: provides a mapping between a URL and a controller action. This is covered in more detail in Rails Routing.

Spring Application Preloader

Although not a configuration file, Spring works closely with Rails configurations and deserves a mention. It is an application preloader that ships with Rails. During development, your application runs in the background. Spring monitors the config and initializers directories for any changes. When a change is detected, it will automatically restart your application. It will also restart your application if any gem dependencies are changed during development.

For More Information

For a comprehensive (and more advanced) guide on configuring Rails, please refer to the Rails Guides.