0
我正在實施一個rails webapp,在這個應用程序中我使用Devise + Omniauth進行身份驗證。在我的Devise模型中,我也使用可確認的模塊。NoMethodError(未定義的方法'持續?'爲true:TrueClass)
現在,當我試圖通過omniauth實現社會登錄Facebook或谷歌,我收到以下錯誤
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
這是代碼來自我的用戶模型的片段
class用戶< 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
錯誤?
這樣做是給**錯誤的參數數目(1 2)**錯誤 –
轉到您的用戶模型,看看如何.from_omniauth方法是定義的還是用設計處理? – miler350
是從用戶模型中刪除current_user參數做的工作..謝謝 –