There have been a lot of blog posts and articles written on how to add application-specific configurations to a Rails app. In version 4.2, there is a method called config_for which makes a lot of that obsolete, simplifying the results.

A common Rails idiom

A seemingly very common idiom is to arrange appliation configuration values in YAML+ERB files under the config directory in a Rails app. Then, in an initializer, read in the file, process it through ERB, then YAML extract the set appropriate to the running environment, and save it in a constant or global:


default: &default

  <<: *default

  <<: *default

  service_url: <%= ENV['APP_SERVICE_URL'] %>


require 'yaml'
require 'erb'

APP_CONFIG = YAML.load('config','app_config.yml'))).result)[Rails.env] || {}

Enter config_for at Rails 4.2

config_for is a method on Rails.application. It can be called bar within all the normal configuration contexts, such as inside config/environments/development.rb and such.

What it does is fairly straightforward, reading a YAML+ERB file from the config directory, returning the appropriate section based on the current Rails environment.

It’s a simple enough implementation: (Note of course this is a volatile link). It’s implemented in Rails::Application#config_for.

You pass in a symbol that corresponds to a file name under config, so config_for(:app_config) loads up the appropriate environment variables from config/app_config.yml.

Now, your initializer can become simply:


APP_CONFIG = Rails.application.config_for(:app_config)


I’ve an example github repo at Feel free to fork it and play with it yourself.