NoMethodError(未定义的方法 `persisted?' for nil:NilClass):
这是我尝试将omniauth与Devise一起使用时遇到的错误......
我可以使用 facebook 注册该网站...但是一旦我注销并尝试重新登录,我就会得到
NoMethodError(未定义的方法 `persisted?' for nil:NilClass):
类模型::OmniauthCallbacksController < 设计::OmniauthCallbacksController
def facebook # 你需要在你的模型中实现下面的方法 (eg 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 => "#{Rails.root}/public/404.html", :status => 404, :layout => false # 或者,# raise ActionController::RoutingError.new('Not Found' ) 结尾
结尾
模型.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