Forum: Ruby on Rails View Question... or is it a model question?

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.
ressister (Guest)
on 2008-11-21 18:45
(Received via mailing list)
I have a partial, that generates a list of categories.  I'd like to
use the same partial for two different scenarios where data will be
coming from different models.  In one case, the partial is grabbing
category data directly from the Category model.  In another case it's
grabbing it from an associated UserCategory model.

Based on the associations, in one scenario, i'd display the category
name like this in the loop: category.name.  In the other scenario
(where I'm first retrieving category info through the UserCategory
model, I'd display it like: category.category.name.

What's the best way to use one partial for both situations?  Do I
alias category name somehow in the model so it can work for both
scenarios?

I look forward to your suggestions.

Thanks!
James M. (Guest)
on 2008-11-21 20:57
(Received via mailing list)
That's probably the 'cleanest' (since non-ugliest isn't really a word)
option.  The next big question is how far do you take it?  How you
actually implement it is where some people tend to like one way over
the other.

You could simply alias the name to a delegated method and call it done.

Or, like in my day job, we actually have 'view state' objects that
take much this kind of complexity out of the controllers and models
which makes things a bit simpler and easier to test.  They are
designed to be loaded up and validated (via Validatable[0] and a
Forwardable[1] extension, if needed), which is then passed along to
the view such that the view does not need to care what kind of
"category" it has or how to get the name from it.

Hope that helps.

[0] http://validatable.rubyforge.org/
[1] http://blog.jayfields.com/2007/02/ruby-forwardable...


--
James M.
ressister (Guest)
on 2008-11-22 00:03
(Received via mailing list)
Thanks James, that was very helpful.

-Alex
This topic is locked and can not be replied to.