NoMethodError(未定义的方法`持久化?'为nil:NilClass):
这是我在尝试使用omiseuth与Devise时遇到的错误...
我可以用facebook注册该网站...但是一旦我退出并尝试重新登录我就得到了
NoMethodError(未定义的方法`持久化?'为nil:NilClass):
class Models :: OmniauthCallbacksController <Devise :: OmniauthCallbacksController
def facebook#您需要在模型中实现以下方法(例如app / models / user.rb)@model = Model.find_for_facebook_oauth(request.env [“omniauth.auth”],current_model)
if @model.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @model, :event => :authentication
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_model_registration_url
end
结束
def passthru render:file =>“# /public/404.html”,:status => 404,:layout => false#或者,#raise ActionController :: RoutingError.new('Not Found' ) 结束
结束
model.rb
class Model < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :provider, :uid
# attr_accessible :title, :body
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
model = Model.where(:provider => auth.provider, :uid => auth.uid).first
unless model
model = Model.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20]
)
end
end
def self.new_with_session(params, session)
super.tap do |model|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
model.email = data["email"] if model.email.blank?
end
end
end
end
2 回答
在找到模型的情况下,
find_for_facebook_oauth
方法隐式返回nil . 如果找到模型,则应显式返回模型 .