[英]NoMethodError(undefined method `persisted?' for true:TrueClass)
我正在實現Rails Web應用程序,在此應用程序中,我使用Devise + Omniauth進行身份驗證。 在我的Devise模型中,我還使用了確認塊。
現在,當我嘗試通過omniauth為facebook或google實現社交登錄時,出現以下錯誤
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
這是我的用戶模型中的代碼片段
類User <ActiveRecord :: 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
誰能幫助我找到為什么我會得到這個“持久化”的未定義方法? 對於true:TrueClass
錯誤?
current_user
幫助程序在omniauth行中扮演什么角色? 我假設它正在與omniauth環境變量產生某種沖突。
嘗試更改:
user = User.from_omniauth(env["omniauth.auth"], current_user)
至:
user = User.from_omniauth(env["omniauth.auth"])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.