【问题标题】:Host app and engine with same model name, rendering host app's model from inside engine using host app's partial, rails主机应用程序和引擎具有相同的模型名称,使用主机应用程序的部分轨道从引擎内部渲染主机应用程序的模型
【发布时间】:2015-06-15 14:42:01
【问题描述】:

我有一个主机应用程序unicorn,型号为Article

我还有一个挂载到名为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,但我认为这行不通。

【问题讨论】:

    标签: ruby-on-rails ruby rails-engines


    【解决方案1】:

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

    @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 %>
    

    【讨论】:

      猜你喜欢
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 2017-06-27
      • 2011-01-28
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多