Friday, 1 June 2012

YAML file configuration in rails application

Hi,

We may required to configure some things in rails application in configuration level. So these things can be configured in YAML file as below.


1) Create your configuration file in config/myconfig.yml

common: &default_settings
  user_settings:
    email: yes
    profile: yes
  product:
    price: yes
    image: yes

development:
  <<: *default_settings


staging:
  <<: *default_settings


production:
  <<: *default_settings


test:
  <<: *default_settings


2) It should be initialised in config/initializers/load_config.rb

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


3) You can access those setting any where in the application
 
   APP_CONFIG['user_settings']['email']

I came to see there is one more useful gem which can do these functionality.  Here is it YETTINGS https://github.com/charlotte-ruby/yettings


Thank You,
Uma Mahesh

No comments:

Post a Comment