1

如何处理不提供某些价值的 Facebook 用户?我正在使用考拉宝石和omniauth。

我收到以下错误:

NoMethodError in SessionsController#create
undefined method `[]' for nil:NilClass

app/models/user.rb:14:in `block in from_omniauth'
app/models/user.rb:5:in `tap'
app/models/user.rb:5:in `from_omniauth'
app/controllers/sessions_controller.rb:3:in `create'

我收到这个错误是因为我的应用程序试图验证的 facebook 用户有一些我的应用程序想要的数据值,比如位置、教育历史等。

一个新的用户进程,在 SessionsController 中开始:

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env['omniauth.auth'])
    session[:user_id] = user.id
    redirect_to root_url, notice: "Signed in!"
  end
end

然后在用户模型中:

def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
  user.provider = auth["provider"]
  user.uid = auth["uid"]
  user.name = auth["info"]["name"] unless auth["info"].blank?
  user.first_name = auth["info"]["first_name"] unless auth["info"].blank?
  user.last_name = auth["info"]["last_name"] unless auth["info"].blank?
  user.image = auth["info"]["image"] unless auth["info"].blank?
  user.email = auth["info"]["email"] unless auth["info"].blank?
  user.gender = auth["extra"]["raw_info"]["gender"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"].blank?)
  user.location = auth["extra"]["raw_info"]["location"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"].blank? && !auth["extra"]["raw_info"]["location"].blank?)          
  user.token = auth["credentials"]["token"] unless auth["credentials"].blank?


    # highschool data
    user.highschool_name = auth["extra"]["raw_info"]["education"][0]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["school"].blank?)
    user.highschool_year = auth["extra"]["raw_info"]["education"][0]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["year"].blank?)
    # graduate school data
    user.graduateschool_name = auth["extra"]["raw_info"]["education"][1]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["school"].blank?)
    user.graduateschool_year = auth["extra"]["raw_info"]["education"][1]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["year"].blank?)

  user.add_friends
  user.save!
      user
 end 
end
end
4

2 回答 2

1

您也可以使用Object.try方法。

user.first_name = auth["info"].try(:[], "first_name")

但最好的方法应该是

unless auth["info"].blank? 
  user.first_name = auth["info"]["first_name"]
  user.last_name = auth["info"]["last_name"]
  user.image = auth["info"]["image"]
  user.email = auth["info"]["email"]
end
于 2012-07-01T19:37:29.830 回答
0

错误在第 14 行为您提供用户模型文件,例如 if auth["credentials"]is nilauth["credentials"]["token"]将给出与您相同的错误。

在分配值时将检查放在一行中,例如,user.token = auth["credentials"]["token"] unless auth["credentials"].blank?

更新

  user.first_name = auth["info"]["first_name"] unless auth["info"].blank?
  user.last_name = auth["info"]["last_name"] unless auth["info"].blank?
  user.image = auth["info"]["image"] unless auth["info"].blank?
  user.email = auth["info"]["email"] unless auth["info"].blank?
  user.gender = auth["extra"]["raw_info"]["gender"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"].blank?)
  user.location = auth["extra"]["raw_info"]["location"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"].blank? && !auth["extra"]["raw_info"]["location"].blank?)
  user.token = auth["credentials"]["token"] unless auth["credentials"].blank?
于 2012-07-01T18:01:49.987 回答