Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much code in a rails view is ok?

I know that it is best to keep code out of the presentation layer. But, I am wondering how much is considered "acceptable". For example I populate an html select box with this line of code.

CodesecureProject.find(:all,:order => 'name').collect {|project| [project.name, project.id] }

Right now I have this line of code embedded in the form. What I am wondering if the community thinks if this is to much code and it should be first stored in an instance variable on the controller then the variable used in the form.

like image 535
Josh Moore Avatar asked Apr 15 '09 09:04

Josh Moore


1 Answers

I'm not going to say I'd never do it (I'd be lying) but the code example given would make me nervous. I think I'd be more inclined to deliver the data to the select box from my controller. A helper method is another option if I notice I'm doing something more than once. I'm more likely to see the duplication in the controller than across distinct views.

If I'm using the same HTML component across multiple views, then I might find myself reaching for partials or wrapping the whole thing in a custom helper: project_select() or some such.

The more I work in the MVC world the more I find myself avoiding code in views. I have a feeling that some kind of Zen mastery will be achieved if I reach the zero code state, although the value of that in anything but philosophical terms is highly debatable.

like image 113
Mike Woodhouse Avatar answered Oct 07 '22 17:10

Mike Woodhouse