rails yaml with expression or variable (Using ERB in YAML Configuration File)
2015-08-22 21:03
751 查看
A while back in Railscast #85 Ryan Bates demonstrated how to add a YAML-based configuration
file to a Rails application. You start with the configuration file — say
containing your configuration data:
And then you load the file from an initializer — say
containing the following:
And from then on a Hash named
be available throughout your application containing the configuration specific to the environment in which your application is running, i.e., development, test or production.
But what if you want to dynamically configure one or more values in your configuration file? Other YAML files loaded by Rails such as fixture files or
processed through ERB before being loaded. Wouldn’t it be nice to be able to do the same in your application configuration file?
As it is Rails will not process those ERB snippets, but you can change that with one small change to your initializer:
Now, instead of loading the file directly, YAML loads the string returned by
will contain the contents of the
the ERB snippets have been evaluated.
file to a Rails application. You start with the configuration file — say
RAILS_ROOT/config/app_config.yml—
containing your configuration data:
# config/app_config.yml development: key1: development value 1 test: key1: test value 1 production: key1: production value 1
And then you load the file from an initializer — say
RAILS_ROOT/config/initializer/load_config.rb—
containing the following:
# config/initializers/load_config.rb APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]
And from then on a Hash named
APP_CONFIGwill
be available throughout your application containing the configuration specific to the environment in which your application is running, i.e., development, test or production.
But what if you want to dynamically configure one or more values in your configuration file? Other YAML files loaded by Rails such as fixture files or
database.ymlare
processed through ERB before being loaded. Wouldn’t it be nice to be able to do the same in your application configuration file?
# config/app_config.yml development: key1: <%= # ruby code ... %> test: key1: <%= # ruby code ... %> production: key1: <%= # ruby code ... %>
As it is Rails will not process those ERB snippets, but you can change that with one small change to your initializer:
# config/initializers/load_config.rb APP_CONFIG = YAML.load(ERB.new(File.read("#{Rails.root}/config/app_config.yml")).result)[Rails.env]
Now, instead of loading the file directly, YAML loads the string returned by
ERB#resultwhich
will contain the contents of the
app_config.ymlafter
the ERB snippets have been evaluated.
相关文章推荐
- 【LeetCode】(70)Climbing Stairs (Easy)
- POJ 1995 Raising Modulo Numbers【快速幂】
- CodeForcesGym 100676G Training Camp
- HN OJ 13375 Flowery Trails (spfa的路径遍历)
- Aizu 2302 On or Off
- poj1363——Rails
- 转:CDC,CPaintDC,CClientDC,CWindowDC区别
- (转载)BeginPaint和GetDC有什么区别?
- HDU2141Can you find it? (二分计算计算Ai+Bj+Ck = X)
- HDU 5344(MZL's xor-(ai+aj)的异或和)
- BFS(最短路) HDOJ 4308 Saving Princess claire_
- hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14
- 百度地图Baidu LBS 的使用
- POJ 2248 & ZOJ 1937 Addition Chains
- Injection of resource dependencies failed解决办法总结
- int main(int argc,char* argv[])详解
- CaptainGan------ 使用“递归方法”对整个目录进行删除操作
- CaptainGan------使用递归方法实现,向FTP服务器上传整个目录结构、从FTP服务器下载整个目录到本地的功能
- NSLayoutConstraint 布局,配合简单的动画效果
- Robot Framework通过Python SMTP进行email收发测试。