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 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)
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!
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.