2

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.

4 Answers 4

8

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.

1
  • This was a nice solution that continued to encapsulate all of the helper logic within a single file while also giving it more structure and form. Thanks! (and thank you Ryan!) Jan 21, 2021 at 17:50
1

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.

7
  • 1
    @Andrew Marshall: I could have been more explicit: the class encapsulates three models, and extracts the bits of information I need for my views. Since it contains instances, a collection of methods alone wouldn't help. And as for "it belongs in the view or view helper", um, it is in a view helper. At least, that's my intention. Mar 3, 2011 at 2:07
  • Are you saying it applies to the views of three models or it takes data from three models? Mar 3, 2011 at 2:09
  • takes data from the three models. Mar 3, 2011 at 2:10
  • It shouldn't be in a view then. The view shouldn't access models directly, that's the controllers job. The view should only access data from models through variables given to it by the controller. Mar 3, 2011 at 2:11
  • Can you clarify? Isn't that like saying a view shouldn't access @user directly, since @user is a model? What I'm doing isn't that different. Mar 3, 2011 at 2:29
1

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.

0
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.