# Generating html

**URL:** <https://www.ruby-forum.com/t/generating-html/188096>\
**Category:** Rails\
**Created:** [May 5, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/generating-html/188096 "2010-05-05T17:38:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam\_wead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@adam\_wead](https://www.ruby-forum.com/u/adam_wead)\
**Post date:** [May 5, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/generating-html/188096/1 "2010-05-05T17:38:40Z")

</div>

Hi all,

I’m new to Ruby/Rails and need someone to help point me in the right  
direction. I have a little inventory app that I built for work to  
help me learn the language. I would like to modular-ize a bit of the  
html code generation and I’m wondering where one places such a thing:  
controller, model, view ?

I’m specifying a certain color for a table based on a couple of  
database fields. Right now, I have the code as a long if/else  
statement in one of the views. As the view loops through the database  
rows, it checks the fields for all the different combination. The  
problem is I’m using it another view as well and want to DRY it up a  
bit. If I were in PHP-land, I’d write a function with a couple of  
database fields as arguments, and it would return some html code or  
just a color, ie. #FFFFFF. I’m not sure how I’d do the same thing in  
Rails. I’m guessing the logic goes in the model and the controller  
has a definition for the function and then the view calls it?

\<% for f in @fields %\>

\<% end %\>

Any guidance would be much appreciated!

many thanks,

…adam

---

<div class="post-metadata">

**Author:** ![adam\_wead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@adam\_wead](https://www.ruby-forum.com/u/adam_wead)\
**Post date:** [May 5, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/generating-html/188096/2 "2010-05-05T18:51:12Z")

</div>

adam wead wrote:

> Hi all,
> 
> I’m new to Ruby/Rails and need someone to help point me in the right  
> direction. I have a little inventory app that I built for work to  
> help me learn the language. I would like to modular-ize a bit of the  
> html code generation and I’m wondering where one places such a thing:  
> controller, model, view ?

The answer is “D.” None of the above.

> I’m specifying a certain color for a table based on a couple of  
> database fields. Right now, I have the code as a long if/else  
> statement in one of the views. As the view loops through the database  
> rows, it checks the fields for all the different combination. The  
> problem is I’m using it another view as well and want to DRY it up a  
> bit. If I were in PHP-land, I’d write a function with a couple of  
> database fields as arguments, and it would return some html code or  
> just a color, ie. #FFFFFF. I’m not sure how I’d do the same thing in  
> Rails. I’m guessing the logic goes in the model and the controller  
> has a definition for the function and then the view calls it?

Anything like this that goes beyond simple conditional logic belongs in  
view helpers. View helpers live inside app/helpers.
