首页 文章

Rails引擎涉及自动加载路径

提问于
浏览
12

我有一些导轨引擎'Core',我有:

# core/app/models/core/concerns/user_helper.rb
module Core
 module UserHelper
  extend ActiveSupport::Concern
  included do
  # some methods
  end
 end
end

# core/app/models/core/user.rb
module Core
 class User < ActiveRecord::Base
  include Core::UserHelper
 end
end

但它说 uninitialized constant Core::UserHelper . 因此,默认情况下引擎似乎没有加载其关注点,因此我将其添加到自动加载路径中

module Core
  class Engine < ::Rails::Engine
    config.autoload_paths += %W(#{Core::Engine.root}/app/models/core/concerns)
    isolate_namespace Core
  end
end

现在我结束了这个错误: Unable to autoload constant UserHelper, expected myapp/core/app/models/core/concerns/user_helper.rb to define it

那么这里有什么问题?当我检查指南http://edgeguides.rubyonrails.org/engines.html并且它没有关注目录中的问题,而是在lib /关注下并且所有引用关注使用Core :: Concerns :: MyConcern时,那么在引擎中存在问题的位置是什么?

谢谢

Edit

Yury评论解释了这个问题,似乎在rails引擎关注目录中没有得到任何特殊处理,并且它被视为模型下的普通目录,因此其中的模块必须在Concerns命名空间内,并且当包含关注时,你如果我理解的话,也必须将其包含在Concerns namesapace中 . 我很惊讶这在文档中没有提到 .

1 回答

  • 20

    问题必须存在于app / models | controllers / concerns / engine_name / concern_name.rb中 . 这将自动加载关注 .

    要包含关注点,请包含EngineName :: ConcernName .

相关问题