10 YAML Configuration Files 0 comments
I've got a useful, quick tip for you this week. Loading in a configuration file can be really handy when you have a few settings that might change in the future of your application, such as contact email addresses or the strength of a spam filter.
We'll load the file in the environment.rb file using YAML. As with all variables we'll give it a name, I'll use SETTINGS, and we'll store it in the config directory.
SETTINGS = YAML.load_file("#{RAILS_ROOT}/config/config.yml")
Back to Finder and just duplicate the database yaml file. Open it up and write your configuration - I'll just setup one setting here called contact email:
contact_email: anyone@example.com
You can now use this in any file, it is just a simple hash, so I'll put it in the footer of my application layout.
<%= SETTINGS['contact_email'] %>
Boot it up, and there we have it.
Download Screencast: Mirror 1 | Mirror 2 | iPhone Size: 13mb, Time: 01:43