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 {

; }

this_month {

; }

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:


<% events_group.each do |event| %>

<% end %>
Date Image Event Name Band Name Venue Description
<%= event.eventDate.to_formatted_s(:short) %> <%=h image_tag event.photo.url(:thumb) %> <%=h event.eventName %> <%=h event.bandName %> <%=h event.venue %> <%=h event.description %> <%= link_to 'Show', event %> <%= link_to "Edit", polymorphic_path(event, :action => :edit) %> <%= link_to "Delete", event, :method => :delete, :confirm => 'Are you sure?', :title => "Delete Event" %>

should the logic to change row color be in here somehow?

Thanks in advance!

Mike

On Mon, Jan 24, 2011 at 7:47 AM, Michael B. [email protected]
wrote:

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,

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

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.

HTH,

Hassan S. ------------------------ [email protected]
twitter: @hassan

On Jan 24, 3:47pm, Michael B. [email protected] wrote:

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

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.

On Jan 25, 11:03pm, Michael B. [email protected] wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs