问题描述
我一直试图弄清楚 Ryan Bates 如何在他的 Facebook 身份验证截屏,正在设置以下FACEBOOK_APP_ID"和FACEBOOK_SECRET"环境变量.
I've been trying to figure out how Ryan Bates, in his Facebook Authentication screencast, is setting the following "FACEBOOK_APP_ID" and "FACEBOOK_SECRET" environment variables.
provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET']
周围有类似的问题,但没有我能够在 Rails 3.2.1 上工作的答案.
There are similar-ish questions around, but no answers that I've been able to get to work on Rails 3.2.1.
更新:
截至 2013 年 5 月,我处理 ENV 变量的首选方法是通过 Figaro gem
As of May 2013, my preferred way to handle ENV variables is via the Figaro gem
推荐答案
你可以看看 评论:
您可以直接在启动服务器的 shell 上设置环境变量:
You can either set environment variables directly on the shell where you are starting your server:
FACEBOOK_APP_ID=12345 FACEBOOK_SECRET=abcdef rails server
或者(相当hacky),你可以在你的config/environments/development.rb
中设置它们:
Or (rather hacky), you could set them in your config/environments/development.rb
:
ENV['FACEBOOK_APP_ID'] = "12345";
ENV['FACEBOOK_SECRET'] = "abcdef";
另一种方式
但是我两者都不做.我会创建一个配置文件(比如 config/facebook.yml
),其中包含每个环境的相应值.然后将其作为常量加载到初始化程序中:
An alternative way
However I would do neither. I would create a config file (say config/facebook.yml
) which holds the corresponding values for every environment. And then load this as a constant in an initializer:
development:
app_id: 12345
secret: abcdef
test:
app_id: 12345
secret: abcdef
production:
app_id: 23456
secret: bcdefg
config/initializers/facebook.rb
FACEBOOK_CONFIG = YAML.load_file("#{::Rails.root}/config/facebook.yml")[::Rails.env]
然后将代码中的 ENV['FACEBOOK_APP_ID']
替换为 FACEBOOK_CONFIG['app_id']
和 ENV['FACEBOOK_SECRET']
FACEBOOK_CONFIG['secret']
.
Then replace ENV['FACEBOOK_APP_ID']
in your code by FACEBOOK_CONFIG['app_id']
and ENV['FACEBOOK_SECRET']
by FACEBOOK_CONFIG['secret']
.
这篇关于在 Rails 3 中设置环境变量 (Devise + Omniauth)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!