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

Norm Scherer 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 Scherer 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,

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,