【问题标题】:How to render a file which is in an included engine in Rails 4.2?如何渲染 Rails 4.2 中包含的引擎中的文件?
【发布时间】:2017-05-06 01:01:55
【问题描述】:

在我的由几个 Rails 引擎组成的 Rails 4.2 应用程序中,需要引擎 itemlized_spendingx 以使用 ajax 调用在引擎 biz_workflowx 中呈现文件。引擎biz_workflowx 包含在itemlized_spendingx 的应用程序控制器中,如下所示:

  module ItemlizedSpendingx

    class ApplicationController < ::ApplicationController

        include BizWorkflowx::WfHelper

    end
  end

在BizWorkflowx::WfHelper 中,定义了一个动作event_action,它在引擎itemlized_spendingx 的索引视图中被称为带有link_to 的ajax。这是 event_action.js.erb 响应 ajax 调用

$("#newworkflow .modal-content").html('<%= j render(:file => "biz_workflowx/application/event_action.html.erb") %>');
$("#newworkflow").modal();

上面evant_action.js.erb 的目的是使用Bootstrap modal 弹出event_action.html.erb 的视图(event_action.js.erb 和event_action.html.erb 都位于biz_workflowx/app/views/application/ 下的同一个子目录中) .我遇到的问题是render(:file =&gt; ..) 会引发template missing 的错误,即使File.file?('biz_workflows/app/views/application/event_action.html.erb) 在ActionView resolver 中返回true。

看起来render 确实很难找到event_action.html.erb,即使File.file? 看到了它。如何让渲染器看到?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 rails-engines


    【解决方案1】:

    这是有效的event_action.js.erb:

    $("#newworkflow .modal-content").html('<%= j render(:file => "/application/event_action.html.erb") %>');
    $("#newworkflow").modal();
    

    去掉biz_workflowx后,就找到了视图模板。看来Rails视图将视图模板解析为相对路径,将event_action.html.erb处理在itemlized_spendingx范围内。

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      相关资源
      最近更新 更多