rhahn on [[IRC]] was asking how to setup a configurable value for his application. After considering using a separate file (possibly using [[YAML]]), it was pointed out that Rails already has config files that can be used.

Okay, enough talk, you came here for the "Howto" part anyway:

This page covers thee different approaches for storing configuration data:

# Use one of the Rails configuration files
# Use your very own file (be in YAML or Ruby)
# Use the database

h2. 1. Use a Rails config file

Rails uses a few config files. You can use these files to set constants and control the configuration of your application. The configuration details are split up between one common file that is shared between the different setups, and individual files that are specific to different setups (eg. development, test & production).

In the config file, add your config details, like this:
<pre>
IMAGES_PATH = "/foo/man/chu/"
</pre>

...and presto-magico! Your config info is available everywhere Rails is!

h3. Version info

h4. Rails 0.9 and newer

Shared settings go in @config/environment.rb@. Settings that aren't shared go into the appropriate file in the @config/environments/@ folder, eg. _config/environments/development.rb_.

h4. Rails 0.8.5

If your setup is the same between test and production, you can use _config/enivornments/shared.rb_. If your config needs to be different between your test environment and your production environment, you will need to add the config details into both `test.rb` and `production.rb` instead.


h3. Accessing

How do you access the config object? There is no "config" object (unless you create one). One approach is to assign config settings to constants in <code>environment.rb</code>, these constants are available all throughout the rails app.

To reference your 'constants' in your Rails app, use the following (in a layout .rhtml file for example):

<pre>
<title>RailsTest: <%= YOUR_SITE_NAME %></title>
</pre>

h3. Restart to reload

You need to restart WEBrick or Apache to load changes made to 'environment.rb'! 

h2. 2.Your very own config file

h3. YAML

Write your config details in a separate file, for example _myconfig.yml_

You can then easily parse and access your config 
<pre>
myconfig = YAML.load(File.open("#{RAILS_ROOT}/config/myconfig.yml"))
</pre>

MichaelSchubert has written about "his use of this technique":http://journal.schubert.cx/articles/2005/04/12/yaml-to-the-rescue

There is also a ConfigurationGenerator to generate a YAML-based configuration system.

h3. Just another ruby file

Place your configuration constants in a regular Ruby file. @include@ the file where you need the settings. For example, this is my @config/email.conf.rb@

<pre><code>
#
# Email configuration
#
$SMTP_ADDRESS		= "smtp.mac.com"
$SMTP_PORT		= 25
$SMTP_DOMAIN		= "mail.mac.com"
$SMTP_USER_NAME		= "johnatl"
$SMTP_USER_PASSWORD	= "secret"
</code></pre>


h2. 3. Use a database table

Put a table in your database, I call mine *preferences*. It has three columns, @id@, @setting@, and @value@. Generate a model for it as usual. In places you need access to the preferences, just @require 'preference'@. In the preference model file, I setup the following:

<pre><code>
# Give global access to the get_settings method
def get_setting(setting)
  return Preference.get_setting(setting)
end

class Preference < ActiveRecord::Base

  def Preference.get_setting(setting)
    result = Preference.find_by_setting( setting )
    if not result.nil?
      return result["value"]
    end
    return nil
  end

end</code></pre>

h2. 3a Configuration Generator

You can also use the Configuration Generator found with the other [[Generators]]. It saves your settings in the database as well as write a YAML each time you update. I make a Configuration for each of my controllers and load that YAML in the respective controller.
category:Howto
