Plugins 可以在Rails 2.3.11中为ActionController::基类定义继承的钩子吗

Plugins 可以在Rails 2.3.11中为ActionController::基类定义继承的钩子吗,plugins,module,ruby-on-rails-2,inherited,development-mode,Plugins,Module,Ruby On Rails 2,Inherited,Development Mode,我正在尝试为ActionController::基类实现继承的方法。我的目的是对继承自ActionController::Base-like ApplicationController的类调用方法,使它们包含某些模块。 此时,我的代码如下所示: module MyModule def inherited(child) end def my_test_method() end end ActionController::Base.send(:extend, MyModule) Actio

我正在尝试为ActionController::基类实现继承的方法。我的目的是对继承自ActionController::Base-like ApplicationController的类调用方法,使它们包含某些模块。 此时,我的代码如下所示:

module MyModule
 def inherited(child)
 end
 def my_test_method()
 end
end
ActionController::Base.send(:extend, MyModule)

ActionController::Base.methods.include? 'my_test_method'
=> true
ActionController::Base.methods.include? 'inherited'
=> false

代码从插件的init文件中调用。

继承的是类的类方法。定义子类时,可以直接覆盖它以添加行为。我不知道如何通过扩展模块来实现这一点,但这应该达到相同的结果:

class ActionController::Base
  def self.inherited(child)
    puts "child created: #{child}"
  end
end

class ChildClass < ActionController::Base
end

Hello LOne,据我所知,您可以向类添加classmethod,并使用模块对其进行扩展。根据我在“my_test_method”案例中的例子,它是有效的。但是为什么它不能与继承的方法一起工作呢?我在其他类,如ActiveRecord::Base上尝试过它,但它成功了。我不知道为什么覆盖可以工作,而扩展不能。
child created: ChildClass