G'day Justin.

> 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.

It might be a one-liner right now, but once you add error handling, it's 
longer. And what's error handling?...It's logic, which doesn't belong in 
views.

> 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.

Yup! Absolutely.

> 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?

There could be all sorts of reasons why the example's writer put the 
call to #find in the view. Regardless, "The Rails Way" is to not put 
logic in views.

> 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 think you might be confusing the act of "fetching" reference data with 
the act of formatting and/or displaying it. Controllers share data (IE: 
variables) with views via instance variables, or passing variables to 
the ":locals" option of #render. That's pretty simple. If multiple 
controller actions need to share the same data, simply write a protected 
or private controller method, or add a method to the model in question, 
that all of these controller actions can call.

> 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 :)

As you know, views shouldn't contain any sort of logic. If you're 
thinking about calling #find within a view, what will you do with the 
data that's returned? Most of the time, you'll need to check if any 
records were returned. After that, you'll iterate through the Array, and 
display some data.

That's "business logic", which doesn't belong in views. But it does 
belong in helpers!
-Nick
-- 
Posted via http://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