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:
First you need to read more docs about how the erb template language works
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 %>">
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.
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'
'hiringProcess'
when 'Job Title Change'
'jobTitleChange'
end
class_attr = value ? %(class="#{value}") : ''
end
class_attr_for will return either class="someValue" or ""