Ruby-on-rails 在 Rails 3 中设置环境变量 (Devise + Omniauth)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11648620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:44:56  来源:igfitidea点击:

Setting Environment Variables in Rails 3 (Devise + Omniauth)

ruby-on-railsruby-on-rails-3environment-variablesomniauthrailscasts

提问by neon

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.

我一直试图弄清楚 Ryan Bates 如何在他的Facebook 身份验证截屏视频中设置以下“FACEBOOK_APP_ID”和“FACEBOOK_SECRET”环境变量。

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.

周围有类似的问题,但没有我能够在 Rails 3.2.1 上工作的答案。

UPDATE:

更新:

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

截至 2013 年 5 月,我处理 ENV 变量的首选方法是通过Figaro gem

回答by iblue

You could take a look at the comments:

你可以看看评论

You can either set environment variables directly on the shell where you are starting your server:

您可以直接在启动服务器的 shell 上设置环境变量:

FACEBOOK_APP_ID=12345 FACEBOOK_SECRET=abcdef rails server

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

或者(相当hacky),您可以将它们设置在您的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),其中包含每个环境的相应值。然后将其作为常量加载到初始化程序中:

config/facebook.yml

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

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

然后更换ENV['FACEBOOK_APP_ID']在你的代码FACEBOOK_CONFIG['app_id'],并ENV['FACEBOOK_SECRET']通过FACEBOOK_CONFIG['secret']

回答by Stefan

There are several options:

有几种选择:

  • Set the environment variables from the command line:

    export FACEBOOK_APP_ID=your_app_id
    export FACEBOOK_SECRET=your_secret
    

    You can put the above lines in your ~/.bashrc

  • Set the environment variables when running rails s:

    FACEBOOK_APP_ID=your_app_id FACEBOOK_SECRET=your_secret rails s
    
  • Create a .envfile with:

    FACEBOOK_APP_ID=your_app_id
    FACEBOOK_SECRET=your_secret
    

    and use either Foreman(starting your app with foreman start) or the dotenvgem.

  • 从命令行设置环境变量:

    export FACEBOOK_APP_ID=your_app_id
    export FACEBOOK_SECRET=your_secret
    

    你可以把上面的几行放在你的 ~/.bashrc

  • 运行时设置环境变量rails s

    FACEBOOK_APP_ID=your_app_id FACEBOOK_SECRET=your_secret rails s
    
  • 创建一个.env文件:

    FACEBOOK_APP_ID=your_app_id
    FACEBOOK_SECRET=your_secret
    

    并使用Foreman(用 启动您的应用程序foreman start)或dotenvgem。

回答by Jan Hettich

Here's another idea. Define the keys and values in provider.ymlfile, as suggested above. Then put this in your environment.rb(before the call to Application.initialize!):

这是另一个想法。provider.yml如上所述,在文件中定义键和值。然后把它放在你的environment.rb(在调用之前Application.initialize!):

YAML.load_file("#{::Rails.root}/config/provider.yml")[::Rails.env].each {|k,v| ENV[k] = v }

Then these environment variables can be referenced in the omniauthinitializer without any ordering dependency among intializers.

然后可以在omniauth初始化程序中引用这些环境变量,而在初始化程序之间没有任何顺序依赖性。