将 Rails Engine 纳入全局布局
我目前正在编写一个模块化 Rails 应用程序,其中每个功能都位于 Rails 引擎内。我已经设置了我的第一个引擎,到目前为止一切正常。现在我想知道将引擎挂接到当前在 app/views/layouts/application.html.haml
中呈现的全局导航的最佳方法是什么,如下所示:
%nav#main-nav
%ul
%li
= link_to "Users", users_path, :class => "no-submenu settings"
%ul
%li ...
我找到的最接近的东西是 spree 的 HookListener
,它使用 deface gem。不幸的是 deface 只适用于 html/erb 输出,因为它使用 nokogiri 解析 DOM,这无论如何都不是最好的主意。
I'm currently writing a modular rails app where every functionality is inside a rails engine. I've set up my first engine and everything's working fine so far. now I'm wondering what is the best way to hook the engine into my global navigation that is currently rendered in app/views/layouts/application.html.haml
, like this:
%nav#main-nav
%ul
%li
= link_to "Users", users_path, :class => "no-submenu settings"
%ul
%li ...
The closest thing I found was the HookListener
of spree, which uses the deface gem. unfortunately deface only works with html/erb output since it parses the DOM with nokogiri, which isn't the best idea anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为记录,我已经这样解决了:
Navigation
类MyEngine 添加到此类中
core/lib/navigation.rb
:myext/lib/myext/engine.rb
:config/navigation.rb
(对于简单导航
):for the record, i've solved it like this:
Navigation
classMyEngine
core/lib/navigation.rb
:myext/lib/myext/engine.rb
:config/navigation.rb
(forsimple-navigation
):在您的应用程序中创建一个模块,您将在每个引擎中包含该模块,例如
MyAppModule
,然后使用
included
挂钩在您的应用程序中注册您的导航等。您可以将所有挂钩等封装在MyAppModule
中,为所有引擎提供一致的集成。create a module in your app, which you'll include in every engine, e.g.
MyAppModule
then use the
included
hook to register your navigation etc with your application. you can encapsulate all your hooks etc within theMyAppModule
, providing a consistent integration for all your engines.