# Basic question about logic in views

**URL:** <https://www.ruby-forum.com/t/basic-question-about-logic-in-views/120443>\
**Category:** Rails\
**Created:** [November 16, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/basic-question-about-logic-in-views/120443 "2007-11-16T23:51:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 16, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/basic-question-about-logic-in-views/120443/1 "2007-11-16T23:51:01Z")

</div>

Another newbie basic question. Here is a made up example:

I will have a 5 x 5 table display with numbers 11, 21, 31, 41, 51, in  
the first row numbers 12, 22, 23, 24, 25 in the second row etc.

The table boxes will display the number of the parking slot and the name  
of the car parked there from a hash I prepared from my car model in the  
controller.

So each cell would have for example:

 ~ 11 ~   
 \<%=h @parking\_lot\_hash[11] if @parking\_lot\_hash[11]%\>

I could do that twenty five times and hardwire the rhtml form…

or I could achieve this in my view with:

 \<% 1.upto(5) do |row| %\> \<% 1.upto(5) do |col| space\_num = row\*10 + col %\> \<% end %\> \<% end %\> 

| ~ \<%= space\_num %\> ~   
 \<%=h @parking\_lot\_hash[space\_num] if @parking\_lot\_hash[space\_num] %\> |

The first way would be 25 lines of text, compared to the 7 in the  
second.

But the second makes the computer think harder? And puts more logic in  
the view?

If that table was going to be reloaded 7 times a minute by users  
working on manipulating where cars were parked, would that tilt things  
to the verbose first solution?

Do servers calculate this stuff so effortlessly that I shouldn’t even  
begin to worry about it?

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 16, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/basic-question-about-logic-in-views/120443/2 "2007-11-16T23:55:51Z")

</div>

> ```
> <% 1.upto(5) do |col|
> 
> ```
> 
> The first way would be 25 lines of text, compared to the 7 in the  
> second.
> 
> But the second makes the computer think harder? And puts more logic in  
> the view?

It also makes a lot more sense at least to me. And honestly this is  
_presentation_ logic, which is what should go into a view no? I don’t  
think your violating any rules here and it makes things a lot simpler to  
look at.

> If that table was going to be reloaded 7 times a minute by users  
> working on manipulating where cars were parked, would that tilt things  
> to the verbose first solution?

No. And as soon as the r/s gets high enough you’ll want to cache it or  
do  
something else anyway.

> Do servers calculate this stuff so effortlessly that I shouldn’t even  
> begin to worry about it?

At 7 r/minute I wouldn’t think about it all.

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 17, 2007, 12:51am UTC](https://www.ruby-forum.com/t/basic-question-about-logic-in-views/120443/3 "2007-11-17T00:51:29Z")

</div>

Thanks a lot… I know so little I can spend a lot of time thinking  
about things that are both irrelevant and wrong!
