RoR:在视图帮助程序文件中定义类

发布于 10-19 18:46 字数 383 浏览 3 评论 0原文

我有一个视图帮助程序文件 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 技术交流群。

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

发布评论

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

评论(4

入画浅相思2024-10-26 18:46:25

Railscasts #213(修订版)(仅限订阅者链接,唉)中, Ryan Bates 提供了一个示例,说明如何(以及为何)在助手中包含类。基本要点是:

# app/helpers/calendar_helper.rb
module CalendarHelper
  def calendar(date = Date.today)
    Calendar.new(self, date).render
  end

  class Calendar
    def render
      # Calendar, render thyself
    end
    # ... additional methods called by #render
  end
end

对于那些反对助手中的类的人,您如何看待 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:

# app/helpers/calendar_helper.rb
module CalendarHelper
  def calendar(date = Date.today)
    Calendar.new(self, date).render
  end

  class Calendar
    def render
      # Calendar, render thyself
    end
    # ... additional methods called by #render
  end
end

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.

怂人2024-10-26 18:46:25

为什么它需要成为一个类?为什么不只是方法的集合呢?这就是助手:有用方法的集合。业务逻辑不属于助手。不过,如果您想提供更多结构和组织,可以将代码放置在帮助程序文件内的模块中。

您可以将类放入 app/models 中,而不必是 ActiveRecord 类,但在将类放置在那里之前,您应该认真考虑类的用途。

如果它只涉及渲染视图,而不是直接访问数据,那么它属于视图或视图助手。

Why does it need to be a class? Why not just a collection of methods? That's what a helper is: a collection of helpful methods. Business logic does not belong in helpers. You can place your code in a module within the helper file if you want to give some more structure and organization, though.

You can put classes in app/models without it having to be an ActiveRecord class, but you should seriously consider what the purpose of your class is before you place it there.

If it concerns only rendering the view, and not accessing data directly, it belongs in the view or a view helper.

绻影浮沉2024-10-26 18:46:25

您可以通过明确提及助手名称来调用该类

ApplicationHelper::AnalysisSummary.new

,但我认为在助手中包含类不是一个好主意。

You can call the class by explicitely mentioning the helper name

ApplicationHelper::AnalysisSummary.new

But I dont think it is a good idea to have classes in helpers.

幸福丶如此2024-10-26 18:46:25

那么它是一个模块:) 绝对不要在助手中定义类。 Jsut 使用一个简单的模块来完成这项工作。

It's a module then :) Definately do not define classes inside helpers. Jsut use a simple module to do the job.

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