Question

Adding this code to my Application.rb breaks the rake command and gives me the error (below) when reloading the rake tasks. I know it can find the 'social_keys.yml' because if I leave the code in and run the rails application, it finds it and authenticates to facebook just fine.

social_keys = File.join(Rails.root, 'config', 'social_keys.yml')
CONFIG = HashWithIndifferentAccess.new(YAML::load(IO.read(social_keys)))[Rails.env]
CONFIG.each do |k,v|
  ENV[k.upcase] ||= v
end

Error:

Error:[rake --tasks] rake aborted!
NoMethodError: undefined method `each' for nil:NilClass
/Users/cj/RubymineProjects/MyApp/config/application.rb:26:in `<class:Application>'
/Users/cj/RubymineProjects/MyApp/config/application.rb:10:in `<module:MyApp>'

Here is the full code and error: https://gist.github.com/cdesch/11191336

I'm using the snippet code from an example application. I suspect it has something todo with HashWithIndifferentAccess Issue or this similar reference to HashWithIndifferentAccess although I'm not good enough with ruby to really know if they are related. From the error message I can gather that it is having a problem iterating over the hash.

How can I fix this code to work or rewrite it to perform the same functionality? The code should load the 'social_keys.yml' as environment variables to use during the initialization of Devise.

Like this:

 config.omniauth :facebook, ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], { :scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}

If this method isn't the best, Is there a different way to get my social keys in as environment variables?

Was it helpful?

Solution

i assume that the information you have in social_keys.yml is not properly nested under the rails-env key.

if you do this

puts YAML::load(File.read(social_keys))
puts YAML::load(File.read(social_keys))[Rails.env]

the latter should be empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top