【发布时间】:2015-10-15 00:41:22
【问题描述】:
我正在实现一个 rails webapp,在这个应用程序中我使用 Devise+ Omniauth 进行身份验证。在我的设计模型中,我也使用可确认块。
现在,当我尝试通过 facebook 或 google 的omniauth 实现社交登录时,出现以下错误
NoMethodError at /customer/auth/google_oauth2/callback
undefined method `persisted?' for true:TrueClass
这是我的 omniauth_callbacks 控制器的代码
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_filter :authenticate_user!
def all
p env["omniauth.auth"]
user = User.from_omniauth(env["omniauth.auth"], current_user)
if user.persisted?
flash[:notice] = "You are in..!!! Go to edit profile to see the status for the accounts"
sign_in_and_redirect(user)
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
def failure
super
end
alias_method :facebook, :all
alias_method :google_oauth2, :all
end
这是我的用户模型中的代码 sn-p
类用户<:base>
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,:confirmable,:token_authenticatable
def self.from_omniauth(auth, current_user)
authorization = Authorization.where(:provider => auth.provider,
:uid => auth.uid.to_s,
:token => auth.credentials.token,
:secret => auth.credentials.secret).first_or_initialize
if authorization.user.blank?
user = current_user || User.where('email = ?', auth["info"]["email"]).first
if user.blank?
user = User.new
user.name = auth.info.name
user.email= auth.info.email
if auth.provider == "twitter"
user.save(:validate => false)
else
user.skip_confirmation!
user.save(:validate => false)
end
end
authorization.username = auth.info.nickname
authorization.user_id = user.id
authorization.save
end
authorization.user
end
结束
我的用户模型有许多授权模型(创建用于处理来自多个提供商的帐户)
class Authorization < ActiveRecord::Base
belongs_to :user
after_create :fetch_details
def fetch_details
self.send("fetch_details_from_#{self.provider.downcase}")
end
def fetch_details_from_facebook
graph = Koala::Facebook::API.new(self.token)
facebook_data = graph.get_object("me")
self.username = facebook_data['username']
self.save
end
end
谁能帮我找出我得到这个的原因 未定义的方法“持续存在?”为真:TrueClass
错误?
【问题讨论】:
标签: ruby-on-rails devise omniauth