# NameError - Problems with if-else

**URL:** <https://www.ruby-forum.com/t/nameerror-problems-with-if-else/186567>\
**Category:** Rails\
**Created:** [April 8, 2010, 11:57pm UTC](https://www.ruby-forum.com/t/nameerror-problems-with-if-else/186567 "2010-04-08T23:57:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [April 8, 2010, 11:57pm UTC](https://www.ruby-forum.com/t/nameerror-problems-with-if-else/186567/1 "2010-04-08T23:57:29Z")

</div>

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 %\>  
  
6:

  
7: \<%= h article.gender %\>  
8: \<%- if (h article.gender = Male) -%\>  
9: 

  
10: \<% else %\>  
11: 

I hope somebody can help me.

Thanks  
Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [April 9, 2010, 12:02am UTC](https://www.ruby-forum.com/t/nameerror-problems-with-if-else/186567/2 "2010-04-09T00:02:10Z")

</div>

On Apr 8, 8:31 pm, Mark [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 5: \<% for article in @articles %\>  
>   
> 6:
> 
>   
> 7: \<%= h article.gender %\>  
> 8: \<%- if (h article.gender = Male) -%\>  
> 9: 
> 
>   
> 10: \<% else %\>  
> 11: 
> 
> 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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [April 9, 2010, 11:31pm UTC](https://www.ruby-forum.com/t/nameerror-problems-with-if-else/186567/3 "2010-04-09T23:31:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [April 9, 2010, 12:58am UTC](https://www.ruby-forum.com/t/nameerror-problems-with-if-else/186567/4 "2010-04-09T00:58:55Z")

</div>

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 %\>

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

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [May 6, 2010, 2:27pm UTC](https://www.ruby-forum.com/t/nameerror-problems-with-if-else/186567/5 "2010-05-06T14:27:49Z")

</div>

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 %\>
> 
> Better yet:
> 
> ...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](http://www.marnen.org)  
> removed\_email\_address@domain.invalid
