6

Im new to RoR/Ruby and i cant seem to get the simplest thing to work. (trust me, ive search google and reread docs, i dont know what wrong)

So in my main view, I added the following:

<%= if 1>2 %>
  <%=     print "helllloooo" %>
<%= else %>
  <%= print "nada" %>
<%= end %>

And nothing is outputted..

**UPDATE**

Ok heres my new CORRECTED code and its STILL NOT WORKING

<th>
  <% if 1 > 2 %>
    <%= print "helllloooo" %>
  <% else %>
    <%= print "nada" %>
  <% end %>  
</th>
ksugiarto
  • 940
  • 1
  • 17
  • 40
Jonah Katz
  • 5,230
  • 16
  • 67
  • 90

5 Answers5

25

Your statements are not intended to be displayed so instead of

<%= if 1>2 %>

write

<% if 1 > 2 %>

Same thing for else and end


EDIT

<% if 1 > 2 %>
<%= "helllloooo" %>  #option 1 to display dynamic data
<% else %>
nada                 #option 2 to display static data
<% end %>
apneadiving
  • 114,565
  • 26
  • 219
  • 213
10

You don't need to use print, or even ERB for the text. Also, your if, else, and end statements should be <%, not <%=:

<% if 1 > 2 %>
helllloooo
<% else %>
nada
<% end %>
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
3

<%= already means "print to the HTML response" in ERB (Ruby's own templating language).

So <%= print '...' means "print the return type of print '...'" which is nothing.

The right code would look like:

<% if 1>2 %>
<%= "helllloooo" %>
<% else %>
<%= "nada" %>
<% end %>

In fact you can even omit the <%= because you're just printing strings (not arbitrary objects):

<% if 1>2 %>
helllloooo
<% else %>
nada
<% end %>
Koraktor
  • 41,357
  • 10
  • 69
  • 99
2

The = is the problem. Use <% instead. <%= is for printing something, while <% is for instructions.

Femaref
  • 60,705
  • 7
  • 138
  • 176
1

for dynamic content use: <%= %>

ankur
  • 79
  • 1
  • 2