[英]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%>
所以發生了兩個問題:
articles
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.