简体   繁体   English

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

[英]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: 所以发生了两个问题:

  1. I can't seem to grab the host app's articles from inside the engine 我似乎无法从引擎内部获取主机应用程序的articles
  2. Even if I did grab the host app's 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.

相关问题 如何在宿主应用程序中呈现Rails引擎的视图 - How to render a Rails engine's views inside the host app 当引擎和主机应用程序具有相同的模型名称时,获取主机应用程序活动记录对象 - Grab host app active record objects when engine and host app have same model names Rails引擎的静态资产未在主机应用程序中提供 - Rails engine's static assets not being served in host app 从Mountable Engine覆盖主机应用程序的根路由 - Override host app's root route from Mountable Engine 如何设置 Rails 引擎语言环境(它使用 :en 甚至强硬的主机应用程序也有不同的语言环境) - How to set Rails engine locale (it's using :en even tough host app has a different locale) Rails Engine仅将中间件添加到主机应用程序 - Rails Engine add middleware to host app only 在Google Cloud App Engine中托管Rails App并设置环境变量 - Host Rails App in the Google Cloud App Engine and set Environment variables Rails 7 引擎如何使未编译的样式表可用于托管应用程序? - Rails 7 engine how to make uncompiled stylesheets available to host app? Rails引擎:使主机应用javascript可用于Mounted Engine - Rails Engines: Make host app javascript available to Mounted Engine Rails主机应用程序看不到引擎布局 - Rails host app doesn't see engine layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM