Rails中用YML配置项目
1 2 3 4 5 6 7 8 9 10 11 12 |
# 在config/initializers中新增文件 app_config.rb module ApplicationConfiguration require 'ostruct' require 'yaml' if File.exists?( File.join(RAILS_ROOT, 'config', 'application.yml') ) file = File.join(RAILS_ROOT, 'config', 'application.yml') users_app_config = YAML.load_file file end ::AppConfig = OpenStruct.new users_app_config end |
然后,在config中增加application.yml,在里面以YML方式写入配置,比如:
hello: ‘world’
则,在程序中,可以直接引用: AppConfig.hello。也可以在程序中对配置项赋值,比如 AppConfig.hello = ‘cool’
Posted by devon At June 12, 2008 14:07
请登录以发表评论。