2015-09-28 22 views
0

我使用的是omniauth-facebook寶石。我需要從Facebook獲取性別和電子郵件,但這不會發生。有人可以幫我從這裏出去嗎?性別不能通過omniauth獲取facebook rails 4

用戶模型

class User < ActiveRecord::Base 
    attr_accessible :email, :name, :uid, :provider, :gender, :oauth_token, :oauth_expires_at  
    validates_presence_of :name, :uid 
    validates_uniqueness_of :uid  

    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.email = auth.info.email 
     user.gender = auth.info.gender 
     user.image = auth.info.image    
     user.save! 
     end 
    end 
end 

用戶位指示

class UsersController < ApplicationController 
respond_to :html, :json 
def create 
@user = User.from_omniauth(env["omniauth.auth"]) 
session[:user_id] = @user.id  
    redirect_to api_browsing_screen_url  
end 
end 

omniauth.rb

OmniAuth.config.logger = Rails.logger 

Rails.application.config.middleware.use OmniAuth::Builder do 
if Rails.env.production? 
    provider :facebook, 'app_id', 'app_secret_key' 
elsif Rails.env.development? 
    provider :facebook, 'app_id', 'app_secret_key', {:scope => 'publish_actions,email', :client_options => { :ssl => { :ca_file => "#{Rails.root}/config/ca-bundle.crt" }}} 
else 
    provider :facebook, 'app_id', 'app_secret_key' 
end 
end 

回答

1

您可以從extra.raw_info.gender獲得性別屬性。像下面

user.gender = auth.extra.raw_info.gender 
+0

不,它沒有 – Saravana

+0

@Saravana我可以嘗試,一旦 –

+0

毗溼奴,我接着說:auth.extra.raw.gender'它說'未定義的方法「性別」的答案更新nil:NilClass',如果我加了'extra.raw_info.gender'什麼都沒有發生。 – Saravana

相關問題