DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

App Level Configuration For Your Rails Apps

08.04.2005
| 14543 views |
  • submit to reddit
        Hard coding stuff into your view templates isn't a great idea, and sometimes you want it out of your layouts too. If you have variables your app requires, but which may change between deployments, put them in /config/appconfig.yml in YAML format, then put this at the top of your /config/environment.rb:

require 'yaml'

And at the bottom of your /config/environment.rb:

APP_CONFIG = YAML::load(File.open("#{RAILS_ROOT}/config/appconfig.yml"))

Now your YAML variables are accessible anywhere from your controllers or views like so:

APP_CONFIG["variable"]

Easy peasy.    

Comments

John Griffiths replied on Mon, 2007/07/23 - 6:25am

nice one!

Snippets Manager replied on Mon, 2012/05/07 - 1:13pm

I use a slightly different version of this that is a combo of the Snippets version (above) and the ERb-aware version taken from environment.rb. I elected to symbolize the keys as well for easy repetitive use. APP_CONFIG = YAML::load(ERB.new((IO.read("#{RAILS_ROOT}/config/settings.yml"))).result).symbolize_keys