如何在配置/初始化程序中使用 YML 值

发布于 2024-11-05 13:04:10 字数 625 浏览 1 评论 0原文

我正在努力将 Facebook 集成到我的应用程序中。为此,我使用 koala、devise 和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:

/config/initalizers/omniauth.rb

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

这里的问题是我在两个位置重复了相同的值。我怎样才能解决这个问题,以便 app_id 和 Secret_key 只存在于一个地方,并且一个文件引用另一个文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2024-11-12 13:04:10

有一个很棒的 Railscast 可以做到这一点: http://railscasts.com/episodes/85-yaml-configuration-file

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

FACEBOOK_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/facebook.yml")[RAILS_ENV]

然后您可以执行以下操作:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, FACEBOOK_CONFIG['app_id'], FACEBOOK_CONFIG['secret_key']
end

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
作死小能手 2024-11-12 13:04:10

除了上述答案之外,您可能需要使用略有不同的代码来加载文件:

FACEBOOK_CONFIG = YAML.load_file(Rails.root.join("config","facebook.yml"))[Rails.env]

In addition to the above answer you might need to use slightly different code to load the file:

FACEBOOK_CONFIG = YAML.load_file(Rails.root.join("config","facebook.yml"))[Rails.env]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文