Find in controller or view

I have an app that does a number of collection_selects.  There are two general methods of getting the data and displaying it that I am aware of.

You can do a find in the controller and assign the result to an instance variable and do the collection_select stuff in the view.

Alternatively you can put the whole find and collection_select in the view (in erb of course).

To me it seems to clutter up the view to do the find in the view so I prefer it in the controller.  Is there some reason that it would be better to do it all in the view?

I am looking for opinions here on the 'best practice' or does it not matter?

Thanks
Norm



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.

Norm S. wrote:

Is there some reason that it would be
better to do it all in the view?

No.

Ideally, the view is only the presentation layer.

Among other reasons, when you start reusing partials, it will be come
readily apparent why you want to marshal the data in the controller, and
make it available to the view.

Ar Chron wrote:

Norm S. wrote:

Is there some reason that it would be
better to do it all in the view?

No.

Ideally, the view is only the presentation layer.

Among other reasons, when you start reusing partials, it will be come
readily apparent why you want to marshal the data in the controller, and
make it available to the view.

Right. The view never, ever, ever touches the database. If you think
you want it to do so, you’ve got a design problem.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

a tip when using partials: look into :locals option and use it
extensively. Don’t assume that a partial can “see” things from the
controller - specify what you want the partial to see using :locals.
It’ll pay off in the end.

chewmanfoo wrote:

a tip when using partials: look into :locals option and use it
extensively. Don’t assume that a partial can “see” things from the
controller - specify what you want the partial to see using :locals.
It’ll pay off in the end.

A most excellent tip. :object and :collection are also very helpful.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]