简体   繁体   中英

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 .

I also have a mountable engine hooked into the host app called blorgh . It also has a model: Article . It is namespaced, so the table name for the engine's Article is actually 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.

#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
  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 . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM