繁体   English   中英

具有相同模型名称的主机应用程序和引擎,使用主机应用程序的部分,导轨从引擎内部渲染主机应用程序的模型

[英]Host app and engine with same model name, rendering host app's model from inside engine using host app's partial, rails

我有一个模型为Article的主机应用程序unicorn

我还有一个名为blorgh的可挂载引擎挂接到主机应用程序中。 它还具有一个模型: Article 它是命名空间,因此引擎的Article的表名称实际上是blorgh_articles.

我想做的是:从引擎内部,我想找到主机应用程序的所有articles ,然后进行渲染。

#controllers/blorgh/articles_controller.rb
require_dependency "blorgh_application_controller"
module Blorgh
  class ArticlesController < ApplicationController
    def index
      @articles = Article.all #properly grabs all the engine's articles
      @host_app_articles = main_app.Article.all # this doesn't work and I don't know why
    end
    ...
  end
end 

然后查看:

#views/blorgh/articles/index.html.erb
<p> Here I will render blorg's articles </p>
<%= render @articles %>

<p> Here I want to render the host app's articles </p>
<%= render main_app.@host_app_articles%>

所以发生了两个问题:

  1. 我似乎无法从引擎内部获取主机应用程序的articles
  2. 即使确实从引擎内部抓取了主机应用程序的articles ,我也不知道如何使用主机应用程序的_article.rb呈现主机应用程序的文章。 在普通的应用程序中,我只会render @host_app_articles但是由于视图位于宿主应用程序中,所以我认为我会render main_app.@host_app_articles但我认为render main_app.@host_app_articles

从引擎中获取主持人的文章:

@host_app_articles = ::Article.all  #refers to top-level namespace class

从引擎内部的视图渲染它:

<% @host_articles.each do |article| %>
  <%= render file: "/app/views/articles/_article", locals: {article: article} %>
<% end %>

只是为了完成,下面是主机应用程序中的局部视图:

#unicorn/app/views/articles/_article.rb
<%= article.title %> <br> <%= article.text %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM