2017-01-05 26 views
0

我試圖從Omniauth獲取用戶地址,但沒有工作。Rails Facebook Omniauth獲取用戶地址

我在登錄後在更新回調中添加了他們的地址。

如果我從omniauth中刪除了他們的地址,那麼應用程序沒有更新他們的地址。

有人有任何想法如何獲得他們的地址,以及爲什麼應用程序沒有編輯和更新他們的地址後登錄?

感謝的

def omniauth_callback 
    auth_hash = request.env['omniauth.auth'] 



    user = User.find_by_uid(auth_hash[:uid]) 
    if user.nil? && auth_hash[:info][:email] 
     user = User.find_by_email(auth_hash[:info][:email]) 
    end 

    if user.nil? 
     email_domain = '' 
     email_domain = '@facebook.com' if auth_hash[:provider] == 'facebook' 
     user = User.new(email: auth_hash[:info][:email] || auth_hash[:info][:nickname] + email_domain, name: auth_hash[:info][:first_name] || '', surname: auth_hash[:info][:last_name] || '', gender: 'I') 

     user.password_digest = '' 
     user.save!(validate: false) 
    end 

    user.update_attribute(:login_at, Time.zone.now) 
    user.update_attribute(:address) 
    user.update_attribute(:neighborhood) 
    user.update_attribute(:postal_code) 
    user.update_attribute(:ip_address, request.remote_ip) 
    user.update_attribute(:provider, auth_hash[:provider]) 
    user.update_attribute(:uid, auth_hash[:uid]) 
    user.update_attribute(:oauth_token, auth_hash[:credentials][:token]) 
    user.update_attribute(:oauth_expires_at, Time.at(auth_hash[:credentials][:expires_at])) 

    cookies[:auth_token] = { value: user.oauth_token, expires: user.oauth_expires_at} 
    redirect_to root_url 
    end 
+0

[Ruby on Rails Omniauth臉書不會返回電子郵件]可能的重複(http://stackoverflow.com/questions/31954540/ruby-on-rails-omniauth-facebook-doesnt-return-email) – OneNeptune

+0

@OneNeptune ,沒有。因爲不是地址 – jjplack

+0

Facebook是不是提供電子郵件的問題,或者您的代碼沒有保存提供的電子郵件? – mysmallidea

回答

0

一個原因你的代碼將無法正常工作,因爲這

user.update_attribute(:address) 

不會做任何事情 - 除了引發錯誤。 You have to pass a value into update_attribute as well as specify the field

同樣@mysmallidea指出,你最好建議use update,因爲這將允許你更新一個數據庫操作中的多個字段。

如果存在,地址數據將在auth_hash之內。所以我建議你先制定出這個散列的結構。在你的開發環境,添加以下內容:

Rails.logger.info auth_hash.inspect 

將輸出電流auth_hash到日誌/ development.log。用它來確定地址數據在散列中的位置。然後,您可以執行如下操作:

user.update address: auth_hash[:info][:address] 

但是,您可能會發現該地址未包含在由facebook oauth系統返回的數據中。在這種情況下,您需要返回documentation以查看是否有可能。

相關問題