2012-04-04 62 views
1

我正在將OmniAuth Facebook整合到我的網站上。我有它的工作,但後來我想添加更多的用戶模型。這裏是我的user.rbOmniAuth + Facebook:無法將字符串轉換爲整數

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

它看起來像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。

+0

嗨,看起來這是正確的。我評論了關於工作和教育的路線,它很有用。如何編輯user.rb來考慮數組? – Victor 2012-04-04 13:35:53

+0

答覆修正。 – 2012-04-05 06:25:25