在ApplicationController rails类中调用super

时间:2017-10-16 17:17:09

标签: ruby-on-rails ruby

我有这段代码:

class ApplicationController < ActionController::Base
  def authenticate_user!(*_args)
    super
  end
end

authenticate_user!我致电super,我应该从authenticate_user!拨打ActionController::Base,这是一个抽象类。

我无法在authenticate_user!内找到ActionController::Base,所以我想有一些元编程涉及(?)。

来自Devise的authenticate_user!如何在ActionController::Base结束?或者在super中使用ApplicationController调用的任何其他方法?

1 个答案:

答案 0 :(得分:0)

authenticate_user!是由devise动态构建的ID的方法,因此您必须使用补丁devise。控制器和视图中都有助手,这可能是您没有按ActionController::Base

的原因

如果您需要覆盖功能,则需要修补此课程:https://github.com/plataformatec/devise/blob/88724e10adaf9ffd1d8dbfbaadda2b9d40de756a/lib/devise/controllers/helpers.rb#L148

您可以通过定义自己的方法来覆盖authenticate_user!方法。首先,您必须创建目录lib/devise/controllers并添加一个名为helpers.rb的文件,您可以添加:

模块设计   模块控制器     模块助手

  module ClassMethods
    def authenticate_user!
      # if something
      # do something
      # else
        super
      # end
    end
  end

如果您没有使用devise,请发表评论,我会删除此答案