Rails 引擎中的助手
我正在开发一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将此代码放入engine.rb中:
Put this code in engine.rb:
要从引擎的视图访问主应用程序助手(ApplicationHelper),我尝试包括以下内容:
app/helpers/your_engine/application_helper.rb
它可以工作,但是有一次,当我重新启动开发服务器时,它抛出了我
未初始化的常量 ActionView::Helpers::ApplicationHelper
,但我无法重现此异常。编辑
删除了这个
include
并制作了这个:lib/my_engine/engine.rb(它在引擎内部)
To access main app helpers (ApplicationHelper) from engine's views I tried include this:
app/helpers/your_engine/application_helper.rb
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)
添加此以防万一:
我在 Rails 7 中使用 Administrate gem 时遇到了同样的问题。我想访问我的主应用程序帮助模块。
只需在
Admin::ApplicationController
中添加helper all_helpers_from_path 'app/helpers'
即可解决此问题。您可以在此处找到官方文档。我的文件现在如下所示:
我在此处找到了答案。
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'
inAdmin::ApplicationController
solved this. You can find the official documentation here.My file now looks like this:
I found the answer here.