【问题标题】:Null pointer error in devise设计中的空指针错误
【发布时间】:2012-11-05 11:18:28
【问题描述】:

我的设计设置发生了一些非常奇怪的事情。

我有一个自定义的 ConfirmationsController,我将其用于设计的自定义行为。

我的客户模型:

class Customer
include Mongoid::Document

after_create      :prepare_demo_app

devise :validatable,
       :registerable,
       :database_authenticatable,
       :confirmable,
       :trackable,
       :rememberable,
       :recoverable,
       :mailchimp

## Confirmable
field :confirmation_token,   :type => String
field :confirmed_at,         :type => Time
field :confirmation_sent_at, :type => Time

我的自定义控制器

 class ConfirmationsController < Devise::ConfirmationsController
    def create
        puts "resource #{resource}"
        puts "resource name #{resource_name}"
        super
    end
enter code here

我的路线.rb

 devise_for :customers,
               :controllers => {:confirmations => "confirmations" },
               :skip => [:sessions, :passwords], :format => false do
               #... other gets and posts but nothing to do with confirmations 
               #...
    end

当我根据路由文件点击此控制器时,我输入了一封电子邮件。单击提交并获取资源的空指针。但不是资源名称。有没有人知道我可能会错过什么或为什么客户会以 null 的身份出现?

【问题讨论】:

    标签: ruby-on-rails ruby devise confirmation devise-confirmable


    【解决方案1】:

    resource 没有在任何地方设置。在default ConfirmationsController 中,Devise 像这样初始化resource(取决于您运行的版本):

    self.resource = resource_class.send_confirmation_instructions(resource_params)
    

    如果你把super放在你的puts语句之前,resource应该被设置。

    【讨论】:

    • 恐怕这是不可能的。一旦调用 super ,就会调用空指针异常。即使只有 create 方法后跟 super,也会发生这种情况。然而值得注意的是,resource_params 也是 nil。
    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多