Ruby on rails 如何使用';行动前';在模块中

Ruby on rails 如何使用';行动前';在模块中,ruby-on-rails,module,gem,Ruby On Rails,Module,Gem,我想在模块中使用“动作前” 不幸的是,我无法让它工作 我在谷歌搜索,但我发现的一切都不能解决问题 我的模块文件如下所示: module ShowController include SimpleController #before_action :set_object, only: [:show] def show set_object end end 我想使用outcommented before_操作行而不是show方法 因此,我试图包括以下模块: includ

我想在模块中使用“动作前”

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

我在谷歌搜索,但我发现的一切都不能解决问题

我的模块文件如下所示:

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

  def show
   set_object
  end
end
我想使用outcommented before_操作行而不是show方法

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

  include AbstractController::Callbacks
  include ActiveSupport::Callbacks
  include ActiveSupport::Concern
  include ActiveSupport
此外,我还试图“要求‘主动支持/全部’”或核心支持

我收到的错误消息是:

 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
class SomeController
MyModel(id:uuid,name:string)的NoMethodError:before_action(未定义的方法):类,你的意思是什么?在提交之前,在我的
MyModel
控制器中考虑这个问题。我的示例用于控制器,而不是模型。如果你在模型中使用这个,然后您可以使用ActiveRecord回调,如提交前或保存后等。抱歉,我解释得有点不好-我实际上是在控制器问题中进行的,但我现在发现我不小心将控制器问题命名为与其他模型问题相同的名称,因此给了我错误:P my bad。如果您得到
未定义的方法“class_属性”
您可能需要将ShowController设置为类而不是模块。