Nick,

That's a great suggestion. I would actually prefer using a helper
method over putting the call to ModelObject#find in the view itself.
Something about calling find directly from the view just doesn't feel
right to me. The only reason I'm not 100% opposed to it is because a
call to find is usually going to be a 1-liner. Even so, it still just
feels like it doesn't belong directly in the view itself.

I am new to Rails, but based on my perception of MVC, fetching data
from the model is a task that is meant to be done by the controller,
and the view should simply be handed that data by the controller.
However, looking through the Rails documentation itself, I saw the
same method mentioned by Elias (calling find directly in the view) in
examples within the ActionView::FormOptionsHelper class, so I guess
that is a Rails convention? Or was it just done for example purposes?

If you start having to order your reference data, and do other
operations where it's not as simple as a call to find(:all), then I
think that could really muddy up the view code. A helper method would
certainly be preferable, perhaps something like category_select(...),
which handles grabbing the data from the model. At least you're not
shoving the find call directly in to the view, and you still gain the
benefit of not having to call the same find() method from all over the
controller.

So far, I have been absolutely blown away by almost all the features
that Rails has to offer, but it almost seems like the designers of the
framework overlooked, or didn't feel it was important, to add a
simple, and DRY, way to fetch reference data from the controller. I am
quite surprised by this, as it is a very common task to fetch
reference data for the purpose of populating controls such as select/
drop-down boxes. I was also surprised at the lack of message board and
blog posts about this subject. Perhaps, as I mentioned, the method
that Elias made reference to is the Rails convention, and I am just
missing something :)

Thanks to everyone for all the great suggestions. Keep em coming.

Regards,

Justin

On Nov 11, 11:15 am, Nick Hoffman <[EMAIL PROTECTED]>
wrote:
> Elias Orozco wrote:
> > Hey Justin,
>
> > I'll recommend that every data used to load drop down lists on the
> > views should be loaded on the views, that avoids repetition on the
> > controllers. For example a list of post categories you can load it
> > like this:
>
> > <%= collection_select(:post, :post_category_id,
> > PostCategory.find(:all), :id, :name, {}) %>
>
> > Hope it helps,
>
> > El as
>
> That's a good suggestion, Elias. I'll add that, if the drop-down list is
> going to be in multiple views, I'd put it in a helper method.
> -Nick
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to