可安装引擎的rails prepend_view_path [英] rails prepend_view_path of mountable engine

查看:94
本文介绍了可安装引擎的rails prepend_view_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一方面,我有一个可安装的引擎,比如说Front 前面包含我的资产和几页 它与MainApp隔离.我不希望它触及主应用程序.

In one hand, I have a mountable engine let's say Front Front contain my assets and couple of pages It's isolated from MainApp. I don't want it to touch the main app.

另一方面,我希望MainApp使用Front的布局和部分内容. 所以我以这种方式设置布局:

In the other hand I want my MainApp using layout and partial of the Front. So I setup the layout this way :

class ApplicationController < ActionController::Base
    layout 'front/application'
end

但是由于这种隔离,前端/应用程序直接引用了引擎部分,就像这样

But the front/application refer to engine partial directly, because of isolation, like this

render 'header' # front/ prefix is not required

因此MainApp视图尝试加载app/views/application/header而不是app/views/front/application/header

So the MainApp views try to load app/views/application/header instead of app/views/front/application/header

要解决这个问题,我像这样放置了一个prepend_view_path:

To fixe this I put a prepend_view_path like this :

class ApplicationController < ActionController::Base
    layout 'front/application'
    before_filter :prepend_front
protected
    def prepend_front
       prepend_view_path "app/views/front"
    end
end

但是这不起作用,因为引擎路径指向供应商. 引擎将其自身添加到前缀路径列表中:〜/main_app/vendor/private_gems/front-0.0.2/app/views 而我的preprend_front方法创建了这个代码:〜/main_app/app/views/front

But this doesn't work because engine path point into the vendor. The engine add it self this to the prepend path list : ~/main_app/vendor/private_gems/front-0.0.2/app/views And my preprend_front method create this : ~/main_app/app/views/front

我试图强行添加正确的路径(但是看起来很脏):

I tryed to prepend by force the correct path (but it looks so dirty) :

prepend_view_path "#{Rails.root}/vendor/private_gems/front-0.0.2/app/views/front"

我不工作,只是使应用程序崩溃了……

I doesn't work, just crash the app ...

我被困在这里.也许我的设计是错误的?

And I'm stuck here. Maybe my design is wrong?

推荐答案

最后,我删除了isolate属性. 我将视图上移了一个文件夹,因此移出了engine_app文件夹.

Finally I remove the isolate property. I moved my views one folder up, so out of the engine_app folders.

唯一的最终调整是将EngineApp :: Engine.url_helper包含在MainApp应用程序控制器中.

The only final ajustement was to include EngineApp::Engine.url_helper into the MainApp application controller.

前置栈如下:

  • 主应用程序前置路径
  • 主要应用的观看次数
  • 引擎视图

因此它基于引擎,然后在顶部添加主要应用程序内容.

So it's based on the engine, then add main app stuff on the top.

这篇关于可安装引擎的rails prepend_view_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆