Forum: Ruby on Rails Color coding table rows depending on an attribute

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4dfa3542d9933c6fc887dbcea5853a75?d=identicon&s=25 ebrad (Guest)
on 2007-07-09 23:27
(Received via mailing list)
I need to show a different background color depending on the type of
comment posted.  The type of comment is determined from a drop-down
selection.  I figured the simplest way would be to do something like I
have below, but I can't seem to get it to work:

<tr <%= puts '"style = "background: red;"' if comment.comment_type ==
'Hiring Process' %> >

I have 4-5 comment_types that need coloring, so I'll probably move
this into a helper when I'm finished using some form of,

if comment_type == 'Hiring Process'
puts "style = 'background: red;'"
elsif comment_type == 'Job Title Change'
puts "style = 'background: green;'"

A similar application would be a help desk ticket system where high
priority tickets would be shown in a different color.
4dfa3542d9933c6fc887dbcea5853a75?d=identicon&s=25 ebrad (Guest)
on 2007-07-09 23:46
(Received via mailing list)

<tr <%= '"style = "background: red;"' if remark.comment_type ==
'Hiring Process' %> >

a helpful IRC chatter, named gnufied, told me that 'puts' cannot go in
a rhtml.  So, I removed the puts and it worked.
10c122532c00465b809dbf9dc35806a7?d=identicon&s=25 Paolo Negri (Guest)
on 2007-07-09 23:47
(Received via mailing list)
First you need to read more docs about how the erb template language

1) you don't need the puts <%= code %> will print the output of the
code on the page.

2) is not necessary that you include "style =" in the ruby code since
that part is the same in any case. You can do

  <tr style="background: <%= "red" if comment.comment_type == 'Hiring
Process' %>;">

3) instead of writing explicitly the colour you can just set a css
class depending by the comment type, you just need to make the
comment_type a valid string for a css class. By instance you can
substitute spaces with _ and make everything lowercase
  <tr class="<%= comment.comment_type.gsub(' ', '_').downcase %>">
4dfa3542d9933c6fc887dbcea5853a75?d=identicon&s=25 ebrad (Guest)
on 2007-07-09 23:52
(Received via mailing list)
Thanks Paolo,

Yeah, you are right, the puts thing was a blunder on my part. Also,
I'm aware about the CSS styling, but I just like to take baby steps
since this is my first rails app.

That was helpful!


Brad Winchester
Ae82cad40a0caca9c932d45c7a9eb3cd?d=identicon&s=25 Michael Glaesemann (Guest)
on 2007-07-10 00:14
(Received via mailing list)
On Jul 9, 2007, at 16:26 , ebrad wrote:

> end
There are two things I think you could do to clean this up:
1) Don't put so much code within HTML tags. A helper can definitely
help with this.
2) Use class attributes to give your tags semantic mean, and then use
CSS to style them appropriately.

For 1), create a helper, something like (untested):

def class_attr_for(comment_type)
   value = case comment_type
           when 'Hiring Process'
           when 'Job Title Change'
   class_attr = value ? %(class="#{value}") : ''

class_attr_for will return either class="someValue" or ""

Then, you can call it in your view template like:

<tr <%= class_attr_for comment_type%>>

And in your CSS add something like:

.hiringProcess { background-color: red; }
.jobTitleChange { background-color: green; }

This gives you much better separation between your mark up and your
presentation, as well as cleaning up the view template.

Hope this helps.

Michael Glaesemann
grzm seespotcode net
4dfa3542d9933c6fc887dbcea5853a75?d=identicon&s=25 ebrad (Guest)
on 2007-07-10 10:02
(Received via mailing list)
You are right...gotta keep it clean and DRY. I'll definitely do this.

This topic is locked and can not be replied to.