For general application configuration that doesn't need to be stored in a database table, I like to create a config.yml file within the config directory. [rails get yml configurations] For your example, it might look like this:

defaults: &defaults
  base_url: http://localhost:3000

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults

This configuration file gets loaded from a custom initializer in config/initializers:

# Rails 2
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]

# Rails 3+
APP_CONFIG = YAML.load_file(Rails.root.join('config/config.yml'))[Rails.env]

If you're using Rails 3, ensure you don't accidentally add a leading slash to your relative config path.

You can then retrieve the value using:

base_url = APP_CONFIG['base_url']
0条评论 顺序楼层
请先登录再回复