Forum: Ruby on Rails MVC Philosophy

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.
Brian A. (Guest)
on 2007-04-20 05:08
My directory browsing app shows lists of file. If the item is a folder,
show a folder icon and link to the action that shows directory contents.
If the item is a graphics file, show a thumbnail and link to a larger
view. If the item is a non-graphics file, show an appropriate icon and
stream the file if the link is clicked.

The controller creates an array of arrays of items in the current
folder, with flags indicating the type of the item. The view iterates
over the array to create the page.

But the view code looks a lot like programming code, with if-then and
case statements to handle all the possibilities. It *feels* like it
belongs in the controller. I could remove some code into partials and
helpers, but that's really just distributing the ugliness around.

On the other hand, using a controller to build an array of html links
doesn't seem right either.

Philosophically, which way should I go?
Mohit S. (Guest)
on 2007-04-20 05:33
(Received via mailing list)
Brian A. wrote:
> But the view code looks a lot like programming code, with if-then and
> case statements to handle all the possibilities. It *feels* like it
> belongs in the controller. I could remove some code into partials and
> helpers, but that's really just distributing the ugliness around.
>
> On the other hand, using a controller to build an array of html links
> doesn't seem right either.
>
> Philosophically, which way should I go?
>
>
Hmm... Personally, I think the controller should mark it as folder,
graphic or other and the view should have a case statement to pick up
the partial that should be used for rendering the view for it.  The
partial might want to use a helper for things like thumbnails, etc. if
required..

Just my thoughts! :)
Cheers
Mohit.
Andrew Z (Guest)
on 2007-04-20 06:32
Brian A. wrote:
> My directory browsing app shows lists of file. If the item is a folder,
> show a folder icon and link to the action that shows directory contents.
> If the item is a graphics file, show a thumbnail and link to a larger
> view. If the item is a non-graphics file, show an appropriate icon and
> stream the file if the link is clicked.
>
> The controller creates an array of arrays of items in the current
> folder, with flags indicating the type of the item. The view iterates
> over the array to create the page.
>
> But the view code looks a lot like programming code, with if-then and
> case statements to handle all the possibilities. It *feels* like it
> belongs in the controller. I could remove some code into partials and
> helpers, but that's really just distributing the ugliness around.
>
> On the other hand, using a controller to build an array of html links
> doesn't seem right either.
>
> Philosophically, which way should I go?

It is hard to give advise without seeing your code. I think using
helpers and/or partials is the way to go. Hmmm... having flags seems
like a bad code smell. Each item should have its own type and with that
being the case you can do stuff like this:

<%= render :partial => "#{item.class.to_s.downcase}_show_link" %>

Hope this helps.
This topic is locked and can not be replied to.