views:

32

answers:

1

I am trying to build in color-coding in my site for a particular class method, such that some values display in a different color if they are low.

I just defined a class method that translates numbers stored in my database to words that are displayed to my users.

# model.rb
def numbers_explained
  numbers_explained = case number
    when 0 then "Low"
    when 1 then "OK"
    when 2 then "OK"
    when 3 then "High"
  end
end 

The other thing I'd like to do is always display "Low" in red.

Can we 'scope' CSS styles like we do with data? Could I attach something like color:red !important;?

+4  A: 

I don't think there's any fancy-easy way to do this with Rails. This is pure presentation logic, so I'd define a helper method in model_helper.rb to wrap it in a span with a class:

def numbers_explained(model)
  content_tag_for(:span, model, :class => (model.number ? '' : 'low')) do
    model.numbers_explained
  end
end

And add CSS for class low to display it in red.

Matt
Listen to Matt, that is what helper methods are for.
Faisal