NoMethodError(未定义的方法`持久化?'为nil:NilClass):

时间:2012-09-06 19:46:44

标签: ruby-on-rails

NoMethodError(未定义的方法`持久化?'代表nil:NilClass):

这是我在尝试将omniauth与Devise ...

一起使用时遇到的错误

我可以用facebook注册该网站... 但是一旦我退出 并尝试重新登录我得到

NoMethodError(未定义的方法`持久化?'代表nil:NilClass):

class Models :: OmniauthCallbacksController<设计:: 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 =>假     #或者,     #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 个答案:

答案 0 :(得分:2)

在找到模型的情况下,您的find_for_facebook_oauth方法隐式返回nil。如果找到模型,则应明确返回模型。

def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  model = Model.where(:provider => auth.provider, :uid => auth.uid).first
  return model if 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

答案 1 :(得分:2)

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
  model #Add this here
end