簡體   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