Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby template if else statement

<% @players.each do |player| %>
    <% if rad_win == true %>
    <%  @mywin = "Radiant Win" %>
    <%  puts "1111111" %>
    <% elsif rad_win == false %>
    <%  @mywin = "Radiant Loss" %>
    <%  puts "222222" %>
    <% else %>
    <%   @mywin = "Loss" %>
    <% end %>
<% end %>

In my test.html.erb I have the following code. I can see that rad_win is false,but that code block is not executed. i.e there is no 22222222

like image 622
Swapnil Kasliwal Avatar asked Nov 01 '25 20:11

Swapnil Kasliwal


1 Answers

You are using ERB, which requires that any text you wanted displayed must be used with <%= %>

As such:

<% @players.each do |player| %>
    <% if rad_win == true %>
    <%  @mywin = "Radiant Win" %>
    <%= "1111111" %>
    <% elsif rad_win == false %>
    <%  @mywin = "Radiant Loss" %>
    <%=  "222222" %>
    <% else %>
    <%   @mywin = "Loss" %>
    <% end %>
<% end %>
like image 166
Anthony Avatar answered Nov 03 '25 17:11

Anthony