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;''" end A similar application would be a help desk ticket system where high priority tickets would be shown in a different color. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
answer: <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. On Jul 9, 5:26 pm, ebrad <nisguy_...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> 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;''" > end > > A similar application would be a help desk ticket system where high > priority tickets would be shown in a different color.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 %>"> On 09/07/07, ebrad <nisguy_300-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > 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;''" > end > > A similar application would be a help desk ticket system where high > priority tickets would be shown in a different color. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! cheers, Brad Winchester On Jul 9, 5:46 pm, "Paolo Negri" <hungryl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 %>"> > > On 09/07/07, ebrad <nisguy_...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > > > 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;''" > > end > > > A similar application would be a help desk ticket system where high > > priority tickets would be shown in a different color.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Glaesemann
2007-Jul-09 22:13 UTC
Re: Color coding table rows depending on an attribute
On Jul 9, 2007, at 16:26 , ebrad wrote:> <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;''" > endThere 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 "" 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You are right...gotta keep it clean and DRY. I''ll definitely do this. Thanks! On Jul 9, 6:13 pm, Michael Glaesemann <g...-RYEyMNgfJnVLeUupdtUFmg@public.gmane.org> wrote:> On Jul 9, 2007, at 16:26 , ebrad wrote: > > > <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;''" > > 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'' > ''hiringProcess'' > when ''Job Title Change'' > ''jobTitleChange'' > end > class_attr = value ? %(class="#{value}") : '''' > end > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---