如何在配置/启动器中使用YML值 [英] How to use YML values in a config/initalizer
问题描述
对于考拉,我有
/config/facebook.yml
开发:
app_id:123123132123
secret_key:dasadsasd1231231
测试:
app_id: 313131313
secret_key:das132asdads12132
制作:
app_id:dasdsadsadsadsa
secret_key:adsdsa12das123fds21
然后我也有omniauth:
/config/initalizers/omniauth.rb
Rails.application.config.middleware.use OmniAuth :: Builder do
provider:facebook,123123132123,'dasadsasd1231231'
end
这里的问题是在两个位置重复相同的值。我怎么可以干掉这个,以便app_id和secret_key只能存在一个地方,一个文件引用另一个?
想法?谢谢
有一个伟大的轨道运动这样做: http://railscasts.com/episodes/85-yaml-configuration-file
在 omniauth.rb
,您可以添加以下行:
FACEBOOK_CONFIG = YAML.load_file( #{RAILS_ROOT} /config/facebook.yml)[RAILS_ENV]
然后你可以做: / p>
Rails.application.config.middleware.use OmniAuth :: Builder do
提供者:facebook,FACEBOOK_CONFIG ['app_id' ],FACEBOOK_CONFIG ['secret_key']
end
I'm working to integrate facebook in my app. To do that I'm using koala, devise, and omniauth.
For koala, I have
/config/facebook.yml
development:
app_id: 123123132123
secret_key: dasadsasd1231231
test:
app_id: 313131313
secret_key: das132asdads12132
production:
app_id: dasdsadsadsadsa
secret_key: adsdsa12das123fds21
I then also have with omniauth:
/config/initalizers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, 123123132123, 'dasadsasd1231231'
end
Problem here is I have the same value repeated in both locations. How can I dry this up so that the app_id and secret_key only live in one place and one file references the other?
Ideas? Thanks
There is a great Railscast on doing this: http://railscasts.com/episodes/85-yaml-configuration-file
In omniauth.rb
, you can add this line:
FACEBOOK_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/facebook.yml")[RAILS_ENV]
Then you can do:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, FACEBOOK_CONFIG['app_id'], FACEBOOK_CONFIG['secret_key']
end
这篇关于如何在配置/启动器中使用YML值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!