Forum: Ruby on Rails Where to put the code? Helper or Model?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Chris D. (Guest)
on 2006-02-16 09:04
I have a class that generates an HTML calendar.  It's pretty simple,
with three methods of any significance: initialize(start,end),
add_event(event) and display_html

Basically, the way it works is this:

@events = Events.find #events in Feb 06
@cal ='02/06')
@events.each {|event| cal.add_event(event)}

Then in my view:

<%= @cal.display_html %>

Currently, I have a model called Calendar, but I'm wondering if I'm
going about this all wrong. I doubt that my current method is the right
approach, because I started having to bend over backwards to get URL's
into my calendar by passing in (link_to '-cut-', :action => 'show') to
my display_html method so that I could build the URL's that go into the
calendar.  A model shouldn't know about URLs.  Perhaps this class should
be placed in a helper?  That doesn't quite sit right with me either.  Is
there another place for it, perhaps the helper just needs to use the
Calendar objects somehow?

Could I be looking at this problem from completely the wrong direction?

Łukasz Piestrzeniewicz (Guest)
on 2006-02-16 11:22
(Received via mailing list)
Hi Chris,

On 16/02/06, Chris D. <removed_email_address@domain.invalid> wrote:
> Could I be looking at this problem from completely the wrong direction?
Model is definitely not a good place to put html rendering code. I
would go for mixed partial/helper solution.

Create a partial 'app/views/calendar/_display.rhtml' and put calendar
html there. If lot of actual ruby code is necessary to create calendar
put required methods in CalendarHelper and use them FROM PARTIAL ONLY.

Then when you want to use callendar in some controller/action do

1. In controller put a line
  helper :calendar
2. In action.rhtml put a line
  <%= render :partial => 'calendar/display' -%>

Look for :locals option for 'render' method to see how to pass
arguments to the partial.
This topic is locked and can not be replied to.