Rails 引擎中的助手

发布于 2024-12-14 05:43:38 字数 361 浏览 5 评论 0原文

我正在开发一个 Rails 引擎,但我与助手之间存在问题。

显然这是一个已知的“问题”,但没有很多解决方案。问题是我有一个 AuthenticationHelper,我想全局访问它 - 但它不起作用。

读到您可以在 init.rb 但似乎没有任何效果。

您知道使应用程序在引擎中可用的最佳方法是什么吗?

编辑:修复了它 - 只需将代码(来自链接)放入 engine.rb 中即可。

I am working on a rails engine and I have a problem with the helpers.

Apparently this is a known "problem" but there's not a lot of solutions out there. The problem is that I have an AuthenticationHelper which I want to access globally - but it's not working.

I've read that you could add a few lines to your init.rb but it does not seem to have any effect.

Any idea what the best way to make an application available in an engine?

EDIT: Fixed it- Just put the code (from the link) in the engine.rb instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在你怀里撒娇 2024-12-21 05:43:39

将此代码放入engine.rb中:

config.to_prepare do
  ApplicationController.helper(MyEngineHelper)
end

Put this code in engine.rb:

config.to_prepare do
  ApplicationController.helper(MyEngineHelper)
end
盛夏已如深秋| 2024-12-21 05:43:39

要从引擎的视图访问主应用程序助手(ApplicationHelper),我尝试包括以下内容:

app/helpers/your_engine/application_helper.rb

module YourEngine
  module ApplicationHelper
    include ActionView::Helpers::ApplicationHelper
  end
end

它可以工作,但是有一次,当我重新启动开发服务器时,它抛出了我未初始化的常量 ActionView::Helpers::ApplicationHelper,但我无法重现此异常。

编辑

删除了这个include并制作了这个:

lib/my_engine/engine.rb(它在引擎内部)

module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
    config.to_prepare do
      ApplicationController.helper(ActionView::Helpers::ApplicationHelper)
    end
  end
end

To access main app helpers (ApplicationHelper) from engine's views I tried include this:

app/helpers/your_engine/application_helper.rb

module YourEngine
  module ApplicationHelper
    include ActionView::Helpers::ApplicationHelper
  end
end

It works, but once, when I restarted dev server, it throws me uninitialized constant ActionView::Helpers::ApplicationHelper, but I can't reproduce this exception.

EDIT

Removed this include and made this one:

lib/my_engine/engine.rb (it's inside engine)

module MyEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
    config.to_prepare do
      ApplicationController.helper(ActionView::Helpers::ApplicationHelper)
    end
  end
end
草莓酥 2024-12-21 05:43:39

添加此以防万一:

我在 Rails 7 中使用 Administrate gem 时遇到了同样的问题。我想访问我的主应用程序帮助模块。

只需在 Admin::ApplicationController 中添加 helper all_helpers_from_path 'app/helpers' 即可解决此问题。您可以在此处找到官方文档。

我的文件现在如下所示:

module Admin
  class ApplicationController < Administrate::ApplicationController
    before_action :authenticate_admin_user!
    helper all_helpers_from_path "app/helpers"
  end
end

我在此处找到了答案。

Adding this just in case:

I had the same problem using the Administrate gem with Rails 7. I wanted to access my main app helper modules.

Simply adding helper all_helpers_from_path 'app/helpers' in Admin::ApplicationController solved this. You can find the official documentation here.

My file now looks like this:

module Admin
  class ApplicationController < Administrate::ApplicationController
    before_action :authenticate_admin_user!
    helper all_helpers_from_path "app/helpers"
  end
end

I found the answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文