OmniAuth + Facebook:无法将String转换为Integer

时间:2012-04-04 10:48:14

标签: ruby-on-rails-3

我正在将OmniAuth Facebook整合到我的网站上。我有它的工作,但后来我想添加一些用户模型。这是我的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

当我通过Facebook验证时,我知道会收到以下错误:

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'

我发布了user.rb

sessions_controller.rb中的第4行是

user = User.find_by_provider_and_uid(auth [“provider”],auth [“uid”])|| User.create_with_omniauth(AUTH)

1 个答案:

答案 0 :(得分:1)

看起来像auth [“extra”] [“raw_info”] [“work”]实际上是一个数组。

如果你改变:

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

为:

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

那应该是DWYM。