0

我已經使用了一年多的stackoverflow,並且總是通過搜索找到答案。但是,我找不到這個問題的答案,所以這個星期是我的第一個問題:Rails 4 Facebook身份驗證 - 其他信息

我跟隨Ryan Bates RailsCast#360瞭解如何在使用omniauth-facebook gem的Rails中驗證用戶,並且在搜索圍繞很長時間設法適應它的Rails 4.

不幸的是,當我試圖通過拉動first_name和last_name(意思是?)與散列中的其他細節一起發送,我無法讓它工作。我設法得到另一個程序甚至圖像的電子郵件地址,但是first_name和last_name沒有被髮送。我碰到下面的返回,當我在會議#raise request.env["omniauth.auth"].to_yaml創建

--- !ruby/hash:OmniAuth::AuthHash 
provider: facebook 
uid: '105531523151136' 
info: !ruby/hash:OmniAuth::AuthHash::InfoHash 
name: Donna Alajhbhfedabi Bushakwitz 
image: http://graph.facebook.com/105531523151136/picture 
... 

這裏是我的omniauth.rb初始化

OmniAuth.config.logger = Rails.logger 

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, FACEBOOK_CONFIG['app_id'], FACEBOOK_CONFIG['secret'] 
end 

而且我user.rb文件:

class User < ActiveRecord::Base 
    def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user| 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.name = auth.info.name 
     user.first_name = auth.info.first_name 
     user.last_name = auth.info.last_name 
     user.oauth_token = auth.credentials.token 
     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
     user.save! 
    end 
    end 
end 

任何幫助將是大。我一直在試圖弄清楚最近3天有什麼問題。

回答

0

此行只是添加到您的初始化:

info_fields: 'first_name, last_name' 
+0

謝謝!這工作,但我也必須重新啓動服務器。我想我昨天試過,但沒有重新啓動服務器(並且可能在我的代碼中也有其他錯誤)。 但是再次感謝! – Nick