I'm launching a course about Next.js + React.jsLearn more

Rails Built-in Configuration Mechanism

2017-11-23 · 1 min read

Rails 4.2+ has a built-in method to manage configuration as YAML files. There is no need for additional dependencies.

Create a YAML file into config/ directory, e.g.: config/redis.yml.

  port: 6789
  host: localhost
  port: 1111

in config/application.rb:

# config/application.rb
module MyApp
  class Application < Rails::Application
    config.redis = config_for(:redis)

Rails.configuration.redis['port'] will be equal 6789 when RAILS_ENV is set to production and 1111 when RAILS_ENV is set to development. You can also define a default environement.

Optionally, you can assign Rails.configuration to Settings global variable for convenience (less characters).

My Tech Newsletter

Get emails from me about programming & web development. I usually send it once a month