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:
>
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;’”
end
A similar application would be a help desk ticket system where high
priority tickets would be shown in a different color.
First you need to read more docs about how the erb template language
works
you don’t need the puts <%= code %> will print the output of the
code on the page.
is not necessary that you include “style =” in the ruby code since
that part is the same in any case. You can do
;">
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
end
There are two things I think you could do to clean this up:
Don’t put so much code within HTML tags. A helper can definitely
help with this.
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 “”
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.