NoMethodError(nil:NilClassの未定義のメソッド `persisted?'):
これは、Deviseでomniauthを使用しようとしたときに発生するエラーです...
Facebookでサイトにサインアップできます...しかし、ログアウトして再度ログインしようとすると、
NoMethodError(nil:NilClassの未定義のメソッド `persisted?'):
クラス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 => "#{Rails.root} /public/404.html"、:status => 404、:layout => false#または、#アクションコントローラー:: 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