本文介绍了在Rails 3中设置环境变量(Devise + Omniauth)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图弄清楚Ryan Bates在他的

解决方案

你可以看一下:



您可以直接在货架上设置环境变量l您正在启动服务器:

  FACEBOOK_APP_ID = 12345 FACEBOOK_SECRET = abcdef rails server 

或(相当恶作剧),您可以将它们设置在 config / environments / development.rb中

  ENV ['FACEBOOK_APP_ID'] =12345; 
ENV ['FACEBOOK_SECRET'] =abcdef;



另一种方法



不过我会既不做我将创建一个配置文件(说 config / facebook.yml ),它保存每个环境的相应值。然后在初始化器中将其作为常量加载:



config / facebook.yml



 开发:
app_id:12345
secret:abcdef

测试:
app_id: 12345
秘密:abcdef

生产:
app_id:23456
秘密:bcdefg



config / initializers / facebook.rb



 code> FACEBOOK_CONFIG = YAML.load_file(#{Rails.root} /config/facebook.yml)[:: Rails.env] 

然后在代码中替换 ENV ['FACEBOOK_APP_ID'] FACEBOOK_CONFIG ['app_id' ] 和 ENV ['FACEBOOK_SECRET'] by FACEBOOK_CONFIG ['secret'] / p>

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']

There are similar-ish questions around, but no answers that I've been able to get to work on Rails 3.2.1.

UPDATE:

As of May 2013, my preferred way to handle ENV variables is via the Figaro gem

解决方案

You could take a look at the comments:

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

Or (rather hacky), you could set them in your config/environments/development.rb:

ENV['FACEBOOK_APP_ID'] = "12345";
ENV['FACEBOOK_SECRET'] = "abcdef";

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:

config/facebook.yml

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]

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:18