Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails NoMethodError(对于nil:NilClass,未定义方法'persistend?';):_Ruby On Rails - Fatal编程技术网

Ruby on rails NoMethodError(对于nil:NilClass,未定义方法'persistend?';):

Ruby on rails NoMethodError(对于nil:NilClass,未定义方法'persistend?';):,ruby-on-rails,Ruby On Rails,NoMethodError(nil:NilClass的未定义方法“persistend”): 这是我在尝试将omniauth与Desive一起使用时遇到的错误 我可以用facebook注册这个网站。。。 但一旦我注销 然后尝试重新登录我得到了 NoMethodError(nil:NilClass的未定义方法“persistend”): 类模型::OmniAuthCallbackController“#{Rails.root}/public/404.html”,:status=>404,:layo

NoMethodError(nil:NilClass的未定义方法“persistend”):

这是我在尝试将omniauth与Desive一起使用时遇到的错误

我可以用facebook注册这个网站。。。 但一旦我注销 然后尝试重新登录我得到了

NoMethodError(nil:NilClass的未定义方法“persistend”):

类模型::OmniAuthCallbackController<设计::OmniAuthCallbackController

def facebook #您需要在模型中实现以下方法(例如app/models/user.rb) @model=model.find_for_facebook_oauth(request.env[“omniauth.auth”],当前_模型)

结束

def直通 render:file=>“#{Rails.root}/public/404.html”,:status=>404,:layout=>false #或者, #raise ActionController::RoutingError.new('未找到') 结束

结束

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
类模型auth.provider,:uid=>auth.uid)。首先
除非模型
model=model.create(名称:auth.extra.raw\u info.name,
提供者:auth.provider,
uid:auth.uid,
电子邮件:auth.info.email,
密码:design.friendly_令牌[0,20]
)
结束
结束
定义self.new_与_会话(参数,会话)
super.tap do |模型|
如果数据=会话[“designe.facebook_数据”]&会话[“designe.facebook_数据”][“额外”][“原始信息”]
model.email=数据[“电子邮件”]如果model.email.blank?
结束
结束
结束
结束

在找到模型的情况下,您的
find\u for\u facebook\u 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

您是否在模型中实现了
find\u for\u facebook\u oauth
方法?你能展示一下吗?我确实加了一句…我正在浏览omniauth wiki…我会马上发布任何建议,如果有人在旁注中提到,这对该方法来说是个坏名字,因为它不仅仅是在寻找东西。如果
find\u或\u create\u for\u facebook\u oauth
返回模型会给您带来语法错误,那么这会更有意义吗?你确定写的正确吗?我刚刚复制并粘贴了你拥有的/Users/rickyahn/Sites/abatman site/app/models/model。rb:32:语法错误,意外关键字_end,预期$end@user1502223试试这个:“谢谢,这很有效!”。请为youtube保留所有caps内容。你能解释一下你的答案吗?为什么这样做呢?我是Rails的新手,所以我的推理可能是错误的,但我相信
除非
返回
nil
如果
model
不是
nil
,那么您想要返回的是
model
。Ruby返回它最后计算的内容,因此在末尾添加
model
可以确保Ruby返回它。
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
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