Rails应用添加配置的方法
Posted by devon on June 12th, 2008 filed in Rails应用方法一: 使用 app_config 插件
1 | script/plugin install http://svn.jarmark.org/rails/app_config |
1 2 3 4 5 | Rails::Initializer.run do |config| ... config.app_config.hello = 'world' ... end |
程序中,则可以通过 AppConfig.hello 直接引用。
参考: http://jarmark.org/projects/app-config/
方法二:使用 application config
参考: http://agilewebdevelopment.com/plugins/application_config
方法三:通过全局变量
在config/initializers中新增文件 app_config.rb,内容为:
1 2 3 4 5 6 7 8 9 10 | 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’
No related posts.
Leave a Comment