简体   繁体   中英

In Ruby on Rails, what is good way for a site specific config / yaml file?

If a website is to be "cloned" as a different website, so that only the title, header, banner logo, and the products of the site are different, it probably will be good to move all the site-specific data to a config or yaml file.

(probably better if not using environment.rb so that the data is separate from other data, to keep it more self-contained, and easier for Program Management to edit the data)

In Rails 3, what is a good method to do this? I think the data probably will be mostly text, plus some data such as what product data to fetch from the main DB. The color theme / sprite offsets probably will still be best to keep in a separate .sass file.

I doing the following Created a my_config.yaml under config folder with development, test and production settings. Then used a script under initializers to load those settings

Contents of config/my_config.yaml

# This file contains all the Application Configuration
# config/my_config.yml

development:
  greet: Hello Developer
test:
  greet: Hello Tester    
production:
  greet: Hello World

Contents of config/initializers/load_config.rb

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

And now I can use APP_CONFIG['greet'] in my app

If you common setting across all environment then you can do some thing like the following

Contents of config/my_setting.yaml

page_title: Hello World
page_header: This is a test

Contents of config/initializers/load_settings.rb

APP_SETTING = YAML.load_file("#{RAILS_ROOT}/config/my_setting.yml")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM