首页 文章

无法使用Rails应用程序内的引擎中的单元格

提问于
浏览
8

我创建了一个引擎,它提供了一个ui组件作为单元格 . 相应的gem(criteria_operator-ui_component)在lib文件夹中几乎不包含任何代码,因为要使单元格正常运行,我必须在资源路径中工作 . gem的基本文件如下所示:

require 'criteria_operator/ui_component/engine'
require 'cells/rails'

module CriteriaOperator
  module UiComponent
    # Your code goes here...
  end
end

引擎不包含太多:

module CriteriaOperator
  module UiComponent
    class Engine < ::Rails::Engine
      require 'jquery-rails'
      require 'criteria_operator'
      isolate_namespace CriteriaOperator::UiComponent
    end
  end
end

对我来说,看起来宝石甚至不知道细胞,但据我所知,我不允许在lib文件夹之外包含任何东西 . 此外,在项目中测试虚拟应用程序中的单元格工作正常 .

现在我在一个真正的Rails应用程序中使用这个引擎 . 在gemfile中,我包括以下内容:

gem 'criteria_operator' 
gem 'cells'       # i added these three, because `bundler list` didn't show me
gem 'cells-rails' # `cells-rails` and `cells-erb` even though they are listed
gem 'cells-erb'   # as dependencies for the engine
gem 'criteria_operator-ui_component'

我登上了路线

mount CriteriaOperator::UiComponent::Engine => '/criteria_operator-ui_component'

并尝试使用单元格 CriteriaOperator::UiComponent::CriteriaEditor ,就像我在虚拟应用程序中所做的那样 . 里面的erb:

cell('criteria_operator/ui_component/criteria_editor', @op)

或者来自代码:

include Cell::RailsExtensions::ActionController    

def whatever
  cell(CriteriaOperator::UiComponent::CriteriaEditor, @op).call()
end

错误是 ActionView::Template::Error (uninitialized constant CriteriaOperator::UiComponent::CriteriaEditor) .

我究竟做错了什么?我在使用引擎时只是遗漏了什么,或者引擎本身是否以错误的方式实现?如果是这种情况,为什么虚拟应用程序有效?我完全陷入困境,这是我第一次创建Rails引擎以及我第一次使用单元格...

引擎的完整代码(包括虚拟应用程序)可以在GitHub上找到(这是因为如果有人需要其他信息,则不是't supposed to be any advertisement, it') .

1 回答

  • 0

    你正在调用 CriteriaOperator::UiComponent::CriteriaEditor 但该类/模块似乎不存在 .

    CriteriaOperator::UiComponent::Engine 工作正常,因为它是在引擎本身中定义的 .

    我'm guessing that your sample application works because it'使用基于视图的调用,如 cell('criteria_operator/ui_component/criteria_editor') ,这可能与javascript一起使用?如果没有将单元格定义为这样的类,则无法使用"code"版本:https://github.com/trailblazer/cells#cell-class

相关问题