问题描述
我有一个在 Rails 4.1.1 应用程序中使用的模块
模块A扩展 ActiveSupport::关注包括做#一些代码结尾结尾
包含在一个类中
class 一些包括A结尾
这与 application.rb
中的 cache_classes=true
配合得很好.现在,如果我关闭类的缓存,我会在启动服务器时得到 Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)
异常.
既然重新加载类是由 Rails 完成的,应该如何处理这样的问题?
对于任何碰壁的人阅读,解决方案是严格遵守 Rails 自动加载规则.那是
- 删除所有 require/require_relative
- 将需要的路径添加到 Rails 自动加载路径
- 使用正确的名称将文件放在正确的位置,以便 Rails 可以推断出在哪里查找要加载的代码.
更多信息在这里:https://github.com/rails/rails/issues/15767>
I have a certain module which is used in a Rails 4.1.1 application
module A
extend ActiveSupport::Concern
included do
#Some code
end
end
which is included in a class
class Some
include A
end
This works great with cache_classes=true
in application.rb
. Now, if I turn off the caching of classes, I get Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)
exception upson starting the server.
How should one deal with such an issue since reloading the classes is done by Rails?
For anyone hitting the same wall to read, the solution to this is to strictly respect Rails autoloading rules. That is
- Removing all the require / require_relative
- Add needed paths to Rails autoload paths
- Put files at the right places with the right names so Rails can infer where to look for code to load.
More info here: https://github.com/rails/rails/issues/15767
这篇关于无法使用 cache_classes = true 为关注点 (ActiveSupport::Concern::MultipleIncludedBlocks) 定义多个“包含"块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!