chnging colour of table rows

A pointer in the right direction needed please!

I'm still getting my first app going, so this i believe should be a fairly simple problem.

My Rails (3.0.0) app displays tables of events, with columns for Date / Band / venue / image etc.

Currently it display the data in date order, which is great, but I'd also like to colour the table rows according to whether the date is old, within the next month, or after 1 month from now.

I imagine I'll want to make a differnt css class for each of these, with stuff like:

old_date {   <TD BGCOLOR="#ffff00">; }

this_month { <TD BGCOLOR="#ff00ff">; }

etc...

but where in the code should I put the logic that determines which css class to use depending on the date?

The view that displays the data is a simple table that goes through the array:

I'd put a helper method in my model that returns something like (one of) "past", "current", "future" and use those as my css class names.

Then your view can include something like e.g. <td class="<%= event.age %>">

HTH,

I'd write a view helper. you might either write a helper called something like class_for_event (and then use it like <td style=<%= class_for_event event %>) or go for something like td_for_event which would create a td with the right class.

Fred

Thanks guys, got it going now!

Used Hassan's suggestion of putting a method in the model, as I wasn't sure how to make a view helper

@Fred - just out of interest

to make a view helper do I need to make a new file in views/shared? do I then define methods in here as normal? def methodname(args) body end

how do I make it inherit all the functionality needed to compare dates / search the database?

Thanks both for your help!

M

Michael,

Helpers go in rails_root/app/helpers. Look in there. If you used scaffold, You should have files for controller specific helpers and a global helper file named application_helper.rb

HTH, Dan

Cheers Dan, I'd thought those helpers were only available to the controllers, so didn't know i could use them with the views as well, thanks for the insight.

By default, you can only use them in views

Fred