1

我正在我的网站上集成 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

session_controller.rb 中的第 4 行是

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

4

1 回答 1

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。

于 2012-04-04T11:12:43.570 回答