# Best Practices question regarding views & controllers

**URL:** <https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192>\
**Category:** Rails\
**Created:** [March 3, 2006, 11:02am UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192 "2006-03-03T11:02:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 3, 2006, 11:02am UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192/1 "2006-03-03T11:02:38Z")

</div>

Hi List!

I’m reading the Agile book, as many online articles as I can, but  
there’s still some things that are just too hard to answer for yourself.  
In building my application, I often find myself putting code into my  
views of which I am not quite certain it belongs there.

Where do you usually draw the line? Is it okay to, say, loop through an  
array of hashes that represents table column names and whether they need  
a certain colspan and build the table according to that?

Any advice that you might have would be greatly appreciated!

Best regards,  
Raphael S.

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 3, 2006, 11:33am UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192/2 "2006-03-03T11:33:56Z")

</div>

Raphael  
\> Where do you usually draw the line? Is it okay to, say, loop  
through an  
\> array of hashes that represents table column names and whether  
they need

> a certain colspan and build the table according to that?

If it - the code - has no impact on/is not impacted by/has nothing to do  
with the model or the business rules, you can safely put it in the view  
or helper.  
I guess there may be exceptions 🙂

Alain

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 3, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192/3 "2006-03-03T14:16:59Z")

</div>

I’m hardly an expert or anything, but - especially if you’re still  
learning - put it wherever you can make it work for now, and plan to  
refactor later.

I find Ruby/Rails code easy to refactor, in general, so I don’t think  
this is a major chore. And the odds are good that much of what you  
write when you’re learning it (deity knows I am!) is going to want to  
be refactored before too long.

– Joshua

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 3, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192/4 "2006-03-03T15:11:39Z")

</div>

I tend to put code in views that only affects the display of the data  
and which relates to the rendering of links, buttons, etc.

I will frequently take ugly code and turn it into a helper or model  
method just to keep things clean and reusable.

\_Kevin

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 3, 2006, 3:45pm UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192/5 "2006-03-03T15:45:00Z")

</div>

Raphael S. wrote:

> Any advice that you might have would be greatly appreciated!

My general rule of thumb is this:

- If we are modifying the model, or finding resources, it belongs in the  
controller
- If we are iterating through a list to display items, it belongs in the  
view

And for the fuzzy areas where we might have to filter the results, the  
view should simply display the items in a set and the controller would  
do all filtering.

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 3, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192/6 "2006-03-03T16:30:41Z")

</div>

I agree. If I have to do something more than once, it becomes a helper.

On Fri, 2006-03-03 at 14:11 +0000, Kevin O. wrote:

> I tend to put code in views that only affects the display of the data  
> and which relates to the rendering of links, buttons, etc.
> 
> I will frequently take ugly code and turn it into a helper or model  
> method just to keep things clean and reusable.
> 
> \_Kevin

Charlie B.  
[http://www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 3, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192/7 "2006-03-03T16:45:17Z")

</div>

As a rule, code in the view should only concern itself with rendering  
the data. Think about the code and ask yourself what the purpose of the  
code is. If its point is to render the model, it should be fine putting  
it in the template.

If it manipulates the model in any way, take it out. In MVC, the  
application stack’s visibility is downward only with each layer only  
knowing about the one immeadiately below it. Think about it this way:

View  
|  
V  
Controller  
|  
V  
Model  
|  
V  
Data Store

This might be a bit simplistic, but the idea is the view should not see  
the model or the data store. It should only see the controller.  
Furthermore, the none of the layers should see levels above them at all,  
making it strictly top-down visible.

---

<div class="post-metadata">

**Author:** ![rapha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@rapha](https://www.ruby-forum.com/u/rapha)\
**Post date:** [March 3, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/best-practices-question-regarding-views-controllers/76192/8 "2006-03-03T16:40:53Z")

</div>

> Where do you usually draw the line? Is it okay to, say, loop through an  
> array of hashes that represents table column names and whether they need  
> a certain colspan and build the table according to that?

That would be fine, if the table headers were hardcode into the view it  
wouldn’t.

The way I understand it the view should contain as little ‘control’  
coding whilst still allowing for Agile development.

So iterating through a hash/array is fine but filtering out elemnts on  
some basis isn’t - that should be done in the controller or model.

I don’t think the Agile book is the best place to learn about these  
concepts. Tony Marstons written some easy intros to MVC. To progress  
beyond the basics I’d look at some of the threads on Sitepoint.

\_tony
