[英]Host app and engine with same model name, rendering host app's model from inside engine using host app's partial, rails
I have a host app unicorn
with the model Article
. 我有一个模型为Article
的主机应用程序unicorn
。
I also have a mountable engine hooked into the host app called blorgh
. 我还有一个名为blorgh
的可挂载引擎挂接到主机应用程序中。 It also has a model: Article
. 它还具有一个模型: Article
。 It is namespaced, so the table name for the engine's Article
is actually blorgh_articles.
它是命名空间,因此引擎的Article
的表名称实际上是blorgh_articles.
What I am trying to do is this: From inside the engine, I want to find all of host app's articles
and then render them. 我想做的是:从引擎内部,我想找到主机应用程序的所有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
And then the view: 然后查看:
#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%>
So two problems going on: 所以发生了两个问题:
articles
from inside the engine 我似乎无法从引擎内部获取主机应用程序的articles
articles
from inside the engine, I do not know how to render the host app's articles using the host app's partial: _article.rb
. 即使确实从引擎内部抓取了主机应用程序的articles
,我也不知道如何使用主机应用程序的_article.rb
呈现主机应用程序的文章。 In a normal app I would just do render @host_app_articles
but since the view lives in the host app, I figured I would do render main_app.@host_app_articles
but I don't think that works. 在普通的应用程序中,我只会render @host_app_articles
但是由于视图位于宿主应用程序中,所以我认为我会render main_app.@host_app_articles
但我认为render main_app.@host_app_articles
。 Grab the host's articles from the engine: 从引擎中获取主持人的文章:
@host_app_articles = ::Article.all #refers to top-level namespace class
Render it from the view inside the engine: 从引擎内部的视图渲染它:
<% @host_articles.each do |article| %>
<%= render file: "/app/views/articles/_article", locals: {article: article} %>
<% end %>
And just for completion, here is what the partial might look like in the host app: 只是为了完成,下面是主机应用程序中的局部视图:
#unicorn/app/views/articles/_article.rb
<%= article.title %> <br> <%= article.text %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.