我可以在 application_helper.rb 中使用其他帮助器方法吗?

发布于 2024-12-22 10:19:23 字数 160 浏览 1 评论 0原文

我对 app/helpers 中定义的方法的可用性有疑问。

我可以共享帮助程序中的方法(例如,使用 application_helper.rb 中的 my_helper.rb 中定义的方法),还是它们仅限于视图?

I have a question about the availability of methods defined in app/helpers.

Can I share methods in helpers, (for example, use methods defined in my_helper.rb in application_helper.rb), or are they restricted to views ?

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

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

发布评论

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

评论(1

只涨不跌 2024-12-29 10:19:23

如果你在控制器或 application_controller 中有这个,你应该能够做到这一点:

helper :all

但是,让调用其他帮助程序的帮助程序似乎有点太复杂了,通常帮助程序方法应该简短并且几乎没有依赖性,他们应该做一个简单的事情每一个都是为了风景。我想如果你有调用其他助手的助手方法,测试就会变得更加困难。

如果您尝试解决的问题足够复杂,我建议尝试将其移动到 lib 目录中的模块,然后将该模块包含在需要通用功能的帮助程序中。

You should be able to do that if you have this in controller or application_controller:

helper :all

But it seems a little too complex to have helpers that call out to other helpers, generally helper methods should be short simple and have little dependencies, they should do one simple thing for the view, each. Makes it harder to test I suppose if you have helper methods that call out to other helpers.

If the problem you are trying to solve with this is sufficiently complex I'd suggest trying to move it to a module in the lib directory and then include that module in the helpers that need the common functionality.

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