ruby-on-railsconfigurationrailtie

How to define custom configuration variables in Rails?


I was wondering how to add custom configuration variables to a Rails application and how to access them in the controller?

Secondly, I was planning to have S3 support for uploads in my application, if I wanted to add a yaml file with the S3 access, secret key, how do I initialize it in my Rails App and how do I access the values that I have defined in that config file.


Solution

  • Update 1

    Very recommended: I'm going with Rails Config gem nowadays for the fine grained control it provides.

    Update2

    If you want a quick solution, then check Jack Pratt's answer below.

    Although my original answer below still works, this answer is now outdated. I recommend looking at updates 1 and 2.

    Original Answer:

    For a quick solution, watching the "YAML Configuration File" screen cast by Ryan Bates should be very helpful.

    In summary:

    # config/initializers/load_config.rb
    APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
    
    # application.rb
    if APP_CONFIG['perform_authentication']
      # Do stuff
    end