generating html

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 %> <table bgcolor="<%= get_color :arg1 => f.field1, :arg2 => f.field2 %>" border="1"> <!-- etc... --> </table> <% end %>

Any guidance would be much appreciated!

many thanks,

...adam

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.