Hi, i have a problem with if-else. I get this error: "uninitialized constant ActionView::Base::CompiledTemplates::Male" I want to change the background-color for male and female. So i wrote this code with if and else. I think there is a mistake in line 8. How could this work. What do i have to write? 5: <% for article in @articles %><br /> 6: <table> 7: <%= h article.gender %> 8: <%- if (h article.gender = Male) -%> 9: <table style="background-color:#d1e1fa;"> 10: <% else %> 11: <table style="background-color:#111111;"> I hope somebody can help me. Thanks Mark -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 8, 8:31 pm, Mark <mjonas.m...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hi, > > i have a problem with if-else. > I get this error: "uninitialized constant > ActionView::Base::CompiledTemplates::Male" > > I want to change the background-color for male and female. So i wrote > this code with if and else. > I think there is a mistake in line 8. How could this work. What do i > have to write? > > 5: <% for article in @articles %><br /> > 6: <table> > 7: <%= h article.gender %> > 8: <%- if (h article.gender = Male) -%> > 9: <table style="background-color:#d1e1fa;"> > 10: <% else %> > 11: <table style="background-color:#111111;"> >did you want to see if article.gender was the string ''Male''?. If so then you need to get rid of the call to h, use == for comparison ( is for assignment) and write "Male" rather than Male (ie a string literal rather than a constant of the same name) Fred> I hope somebody can help me. > > Thanks > Mark > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
As an unrelated, but hopefully helpful aside, I''d also recommend not duplicating your markup in both the "if" and "else" sections. Doing things this way quickly gets hard to read and maintain. Better might be: <% if (article.gender == "Male") then color = ''#d1e1fa'' else color ''#111111'' end %> <table style="background-color: <%= color %>"> This way you only have to write your table markup once and the conditional part is clearly called out. When you later on decide to add an attribute to your table or replace tables with CSS, or whatever, you have much less work to do. Completely optional, but the more you follow this kind of practice, the easier and faster you''ll keep things running later. I only mention this because I''ve seen extreme examples where people copy and paste huge chunks of template across if/else conditions just to change one or at most a handful of properties. Madness! :) HTH jsw -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ahhh thank you Josh and Frederick, now it works. It was the problem with the "h"... I had tried so many different styles but either i''ve forget the quotation marks or write the "h"... Thank you for your advise. I''m just working on the code so i hope it will be more clearly at the end. Thanks Mark -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Josh _ wrote:> As an unrelated, but hopefully helpful aside, I''d also recommend not > duplicating your markup in both the "if" and "else" sections. Doing > things > this way quickly gets hard to read and maintain. Better might be: > > <% if (article.gender == "Male") then color = ''#d1e1fa'' else color > ''#111111'' end %> > <table style="background-color: <%= color %>">Better yet: <table class="<%= article.gender.downcase %>"> ...then define the colors in your CSS. The style attribute leads to unmaintainable HTML and should be avoided like the plague. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.