Victor
Victor

Reputation: 351

OmniAuth + Facebook: can't convert String into Integer

I am integrating OmniAuth Facebook on my site. I had it working but then I wanted to add a few more user models. Here is my user.rb

class User < ActiveRecord::Base
    def self.create_with_omniauth(auth)
        create! do |user|
            user.provider = auth["provider"]
            user.uid = auth["uid"]
            user.email = auth["info"]["email"]
            user.name = auth["info"]["name"]
            user.first_name = auth["info"]["first_name"]
            user.last_name = auth["info"]["last_name"]
            user.birthday = auth["extra"]["raw_info"]["birthday"]
            user.location = auth["info"]["location"]
            user.hometown = auth["extra"]["raw_info"]["hometown"]["name"]
            user.employer = auth["extra"]["raw_info"]["work"]["employer"]["name"]
            user.position = auth["extra"]["raw_info"]["work"]["position"]["name"]
            user.gender = auth["extra"]["raw_info"]["gender"]
            user.school = auth["extra"]["raw_info"]["education"]["school"]
            user.token = auth["credentials"]["token"]
        end
    end
end

When I authenticate with Facebook, I know get the following error:

TypeError (can't convert String into Integer):
  app/models/user.rb:13:in `[]'
  app/models/user.rb:13:in `block in create_with_omniauth'
  app/models/user.rb:3:in `create_with_omniauth'
  app/controllers/sessions_controller.rb:4:in `create'

I posted user.rb

Line 4 in sessions_controller.rb is

user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)

Upvotes: 1

Views: 1175

Answers (1)

Michael Slade
Michael Slade

Reputation: 13877

it looks like auth["extra"]["raw_info"]["work"] is actually an array.

If you change:

user.employer = auth["extra"]["raw_info"]["work"]["employer"]["name"]
user.position = auth["extra"]["raw_info"]["work"]["position"]["name"]

to:

if auth["extra"]["raw_info"]["work"]
  user.employer = auth["extra"]["raw_info"]["work"][0]["employer"]["name"]
  user.position = auth["extra"]["raw_info"]["work"][0]["position"]["name"]
end

That should DWYM.

Upvotes: 1

Related Questions