如何在模块中使用 'before_action'

How to use 'before_action' in a module

我想在模块中使用 'before_action'。

不幸的是,我无法让它工作。

我在谷歌搜索,但我发现的一切都无法解决问题。

我的模块文件如下所示:

module ShowController
  include SimpleController
  #before_action :set_object, only: [:show]

  def show
   set_object
  end
end

我想使用取消注释的 before_action 行而不是 show 方法。

因此,我试图包括以下模块:

  include AbstractController::Callbacks
  include ActiveSupport::Callbacks
  include ActiveSupport::Concern
  include ActiveSupport

此外,我尝试 "require 'active_support/all'" 或 core_ext。

我收到的error_message是:

 undefined method `class_attribute' for SimpleController::ShowController:Module

最后,没有任何解决办法,我也没有找到解决方案。

我想这就是你想要做的:

class SomeController < ActionController::Base
  include SimpleController
end 

module SimpleController
  extend ActiveSupport::Concern

  included do
    before_action :set_object, only: [:show]
  end
end