Setting Environment Variables in Rails 3 (Devise + Omniauth)

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

Leave a Comment