I have been following the solution provided in a previous question:
Accessing config from application.rb in Controller (Rails 3)
but applying it for Facebook settings first.
I created the facebook config file first:
#lib/facebook_config.rb
module FacebookConfig
def self.config
@@config ||= {}
end
def self.config=(hash)
@@config = hash
end
end
and defined my YAML file then initialised it.
#config/facebook.yml
development:
app_id = "id"
app_secret = "secret"
test:
app_id = "id"
app_secret = "secret"
production:
app_id = "id"
app_secret = "secret"
and
#config/intializers/01.facebook_config.rb
require './lib/facebook_config'
FacebookConfig.config = YAML.load_file("config/facebook.yml")[Rails.env]
and defining the omniauth config as such:
#config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
# The following is for facebook
provider :facebook, FacebookConfig.config[:app_id], FacebookConfig.config[:app_secret], {:scope => 'email, read_stream, read_friendlists, friends_likes, friends_status, offline_access'}
end
however when I am testing in development mode I get the following error when starting up the webrick server:
config/initializers/omniauth.rb:3:in `[]’: can’t convert Symbol into Integer (TypeError)
I thought when accessing the config file it should be a String and not a Symbol?
Thanks for any tips.
Possible reason could be because of your facebook.yml file formatting.
You can read about Yaml here: http://en.wikipedia.org/wiki/YAML
Edited
I have tested your code in dummy app – same error, because, YAML file you have currently, will return string.
So you can not access by
symbol, because Ruby thinks that you are passingNumeric Indexvalue forString.Update your facebook.yml to this one (set your variables):
After facebook.yml update, as result you will get hash:
Symbolize your config hash keys:
And it will work!