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 four different approaches for storing configuration data:
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). See EnvironmentFiles for more information about those files.
In the config file, add your config details, like this:
IMAGES_PATH = “/foo/man/chu/”
…and presto-magico! Your config info is available everywhere Rails is!
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.
>> This doesn’t work. I added a variable called $MYVAR in config/environments/development.rb and when I tried to access it in any of my models or controllers it told me MYVAR was nil.
>> Are you restarting WEBrick/Apache after adding the setting, as directed below? That’s what caught me out… also, you need to consistently use MYVAR without the $ sign.
If your setup is the same between test and production, you can use config/environments/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.
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 environment.rb, 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):
<title>RailsTest: <%= YOUR_SITE_NAME %></title>
You need to restart WEBrick or Apache to load changes made to ‘environment.rb’!
Write your config details in a separate file, for example myconfig.yml
You can then easily parse and access your config
myconfig = YAML.load(File.open(“#{RAILS_ROOT}/config/myconfig.yml”))
There is also a ConfigurationGenerator to generate a YAML-based configuration system.
But if you write ‘myconfig = …’ in environment.rb, you can’t access myconfig from all controllers and templates. You need an additional code.
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
#
# Email configuration
#
$SMTP_ADDRESS = "smtp.mac.com"
$SMTP_PORT = 25
$SMTP_DOMAIN = "mail.mac.com"
$SMTP_USER_NAME = "johnatl"
$SMTP_USER_PASSWORD = "secret"
I had to use require rather than include.
I believe that the proper terminology for items that use ‘$’ is global variable and not really a constant.
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:
# 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
_get_setting should really go in the ApplicationController, shouldn’t it? —Rad Smith_
Actually, it should probably go in the ApplicationHelper, strictly speaking… more often than not, you’ll need to access the configuration from the views (see “page title.”) — Anonymous Coward
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