开发者

Whats wrong with my simple If Else?

开发者 https://www.devze.com 2023-03-25 21:16 出处:网络
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)

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" %>
<开发者_如何学Python%= 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>


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


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


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


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


for dynamic content use: <%= %>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号