加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 具有隔离命名空间的Rails引擎共享布局

发布时间:2020-12-16 19:06:07 所属栏目:百科 来源:网络整理
导读:我有一个 Rails Engine,我想从容器应用程序共享一个布局.我想支持主应用程序布局中的所有URL帮助程序,以使集成变得微不足道.这是为了支持容器应用程序中包含帮助程序的布局: = link_to "Signup",new_user_path= link_to "Login",new_user_path... 这导致:
我有一个 Rails Engine,我想从容器应用程序共享一个布局.我想支持主应用程序布局中的所有URL帮助程序,以使集成变得微不足道.这是为了支持容器应用程序中包含帮助程序的布局:
= link_to "Signup",new_user_path
= link_to "Login",new_user_path
...

这导致:

undefined local variable or method `new_user_path’ for #<#:0x007f9bf9a4a168>

我可以通过将application.html(在容器应用程序中)更改为:

= link_to "Signup",main_app.new_user_path
= link_to "Login",main_app.new_user_path

但目标是使其集成引擎不需要用户对现有功能的application.html进行更改.

我相信我也可以通过从lib / example / engine.rb中删除isolate_namespace示例来修复错误,但这几乎打破了引擎中的所有内容.

任何方式允许容器应用程序帮助程序和显式命名我的引擎帮助程序,以避免冲突? (即使用example.root_path而不是root_path)?

解决方法

看看这个: https://github.com/rails/rails/blob/a690207700d92a1e24712c95114a2931d6197985/actionpack/lib/abstract_controller/helpers.rb#L108

您可以在主机应用中包含引擎中的助手.

module Blargh
  class Engine < ::Rails::Engine
    isolate_namespace Blargh

    config.to_prepare do


      # application helper
      ApplicationController.helper(Blargh::ApplicationHelper)
      # any other helper
    end
  end
end

这样您就可以毫无问题地在rails主机中使用助手.当然,这种方式没有真正的命名空间,因此,如果引擎的用户命名一个与辅助方法相同的新辅助方法,它将发生冲突.

这是否回答你的问题?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读