9

我在我的 Rails 应用程序中编写了自定义工具。我在这样的文件中启用它config/initializers/instrumentation.rb

ActiveSupport.on_load(:action_controller) do
  include FooBar::ControllerRuntime
end

但这会导致我出错A copy of FooBar::ControllerRuntime has been removed from the module tree but is still active!。我想我可以通过两种方式解决它:

  • 添加路径可能是“FooBar::ControllerRuntime is defined toconfig.autoload_one_paths ”
  • 定义:to_prepare回调ActionController::Railtie

第二种解决方案如下所示:

config.to_prepare do
  ActionController.include FooBar::ControllerRuntime
end

这么长的介绍引出了一个问题:哪种方式更好?首先,我禁止重新加载与我的FooBar::ControllerRuntime. 有了第二个,我觉得搞砸是不好的ActionController::Railtie。正确的知道ActionController::Railtie没有定义to_prepare,但是如果在下一个版本中会发生什么?

4

1 回答 1

4

第一种方法看起来更干净 -

添加路径,其中可能“FooBar::ControllerRuntime 被定义为 config.autoload_one_paths”

原因 -

1)如果你真的想在 lib/extensions.rb 等文件中做一些猴子补丁,你可以手动要求它:

在 config/initializers/require.rb 中:

需要“#{Rails.root}/lib/extensions”

2) 遵循正确的命名约定,因为您必须列出 class 和 module 。

虽然我不建议为生产应用程序自动加载,但如果这是最后一个选项,你当然可以尝试一下。

在这里很好地阅读 - http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-production/

于 2016-09-26T07:18:13.117 回答