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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3c152ff54c308d08dd4e0508e051de4e?d=identicon&s=25 Chris Dwan (chrisdwan)
on 2006-02-16 08: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 = Calendar.new('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?

Regards!
-C
6578456ff00ab3520b1fe684bc572b45?d=identicon&s=25 Łukasz Piestrzeniewicz (Guest)
on 2006-02-16 10:22
(Received via mailing list)
Hi Chris,

On 16/02/06, Chris Dwan <dwancpf@shaw.ca> wrote:
[snip]
> 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
following:

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.