如何在配置/初始化程序中使用YML值 [英] How to use YML values in a config/initializer

查看:186
本文介绍了如何在配置/初始化程序中使用YML值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将Facebook集成到我的应用程序中.为此,我使用了考拉,设计和omniauth.

I'm working to integrate facebook in my app. To do that I'm using koala, devise, and omniauth.

对于考拉,我有

/config/facebook.yml

development:
    app_id: 123123132123
    secret_key: dasadsasd1231231
test:
    app_id: 313131313
    secret_key: das132asdads12132
production:
    app_id: dasdsadsadsadsa
    secret_key: adsdsa12das123fds21

然后我也可以使用omniauth:

I then also have with omniauth:

/config/initalizers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 123123132123, 'dasadsasd1231231'
end

这里的问题是我在两个位置都重复了相同的值.我该如何干燥,以使app_id和secret_key仅位于一个位置,而一个文件引用另一个位置?

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?

推荐答案

在此方面,有一个很棒的Railscast: http://railscasts.com/episodes/85-yaml-configuration-file

There is a great Railscast on doing this: http://railscasts.com/episodes/85-yaml-configuration-file

omniauth.rb中,您可以添加以下行:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆