RoR:在视图帮助程序文件中定义类
我有一个视图帮助程序文件 app/helpers/analysis_helper.rb,我一直在各种视图文件中使用其顶级方法。工作正常。然后,我在 Analysis_helper.rb 中定义了一个 AnalysisSummary 类来打包一些特定于视图的功能。
但是,当我尝试在视图文件中实例化 AnalysisSummary 时,我收到错误:
uninitialized constant ActionView::CompiledTemplates::AnalysisSummary
也许 Rails 告诉我不应该在帮助程序文件中定义类?如果是这样,您建议将 AnalysisSummary 停放在哪里?它不是控制器,它不是模型......
谢谢。
I have a view helper file, app/helpers/analysis_helper.rb, whose toplevel methods I've been using in various view files. Works fine. I then defined an AnalysisSummary class inside analysis_helper.rb to package up some view-specific functionality.
However, when I try to instantiate an AnalysisSummary in a view file, I get the error:
uninitialized constant ActionView::CompiledTemplates::AnalysisSummary
Perhaps Rails is telling me that I shouldn't be defining a class inside a helper file? If so, where would you suggest parking AnalysisSummary? It's not a controller, it's not a model...
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在Railscasts #213(修订版)(仅限订阅者链接,唉)中, Ryan Bates 提供了一个示例,说明如何(以及为何)在助手中包含类。基本要点是:
对于那些反对助手中的类的人,您如何看待 Ryan 的选择?助手用于生成标记,对吧?因此,如果类中的所有逻辑都与渲染(相当复杂)HTML 有关,我认为它的作用(而不是它的作用)使其适合包含在内在一个帮手中。
In Railscasts #213 (Revised) (subscribers only link, alas), Ryan Bates provides an example of how (and why) you might include a class within a helper. The basic gist is this:
To those opposed to classes within helpers, what do you make of Ryan's choice? Helpers are for generating markup, right? So if all the logic within class pertains to rendering (rather complicated) HTML, I would think that what it does (as opposed to what it is) makes it appropriate for inclusion in a helper.