0

我正在为 Devise 进行 Facebook 身份验证,我偶然发现了一个问题 - 何时尝试使用系统中已有的 Facebook 电子邮件注册某人时,我收到一个错误(部分正确)。

我需要将此用户重定向回主页并在那里打印此问题,但目前我将此消息打印为错误(在本地主机上)。

这是我的做法(用户模型):

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token.extra.raw_info

    if user = User.where(:provider => 'facebook', :uid => data.id).first
      user
    else # Create a user with a stub password. 
      user = User.create!(:first_name => data.first_name,
                          :last_name => data.last_name,
                          :email => data.email, 
                          :password => Devise.friendly_token[0,20],
                          :provider => 'facebook',
                          :uid => data.id,
                          :terms_of_use => true) 
    end
    return user if user
  end

如何在将打印出验证消息的页面上重定向用户?

谢谢

4

1 回答 1

1

为什么你的模型和控制器中没有这样的东西

Return just the user object from the method

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token.extra.raw_info
    #Look for the first user with provider: :facebook & uid: data.id,
    #If now user is there go ahead and create one with first_or_create.
    user = User.where(:provider => 'facebook', :uid => data.id).first_or_create do |user|
           user.first_name = data.first_name,
           user.last_name = data.last_name,
           user.email = data.email,
           user.password = Devise.friendly_token[0,20],
           user.provider = 'facebook',
           user.uid = data.id,
           user.terms_od_use = true
          end
  end

Controller

def sign_in_method #dummy method
  user = User.find_for_facebook_oauth(access_token, signed_in_resource=nil)
  if user.valid?
    redirect success_url #any url where u want to redirect on success 
  else user.errors
     redirect root_url, error: user.errors.full_messages.join(', ')
  end
end
于 2013-08-01T15:14:02.320 回答