Where to store (structured) configuration data in Rails

For the Rails 3 application that I am writing, I am considering reading some configuration data from XML, YAML, or JSON files on the local file system.

Point: where should I put these files? Is there a default place in Rails applications where this kind of content can be stored?

As a side note, my app is deployed to Heroku.

+5
source share
4 answers

What I always do is:

  • If the file is a shared configuration file: I create a YAML file in the / config directory with one top-class key for each environment.
  • ( ): YAML /config/environment/

, YAML, config , APP_CONFIG

+7

:

a config/config.yml

development:
  another_key: "test"
  app_name: "My App"
test:
  another_key: "test"
production:
  prova: "ciao"

#config/initializer/load_config.rb
require 'ostruct'
config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
::AppSetting = OpenStruct.new(config.send(RAILS_ENV))

DB ,

AppSetting.another_key
AppSetting.app_name


!

+4

, Settings.var_name , .

settingslogic gem:

class Settings < Settingslogic
  source "#{Rails.root}/config/settings.yml"
  namespace Rails.env
end
+1

Rails creates a configdefault directory containing a lot of configuration information for your application, including database and environment information. I think a logical first place to consider.

The second choice is a directory appthat contains all the models, views, and controllers for the application, but I think this directory contains executable code and its templates, so I would go with configthat, personal.

+1
source

All Articles