Ruby-on-rails 我的简单 If Else 有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6932663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Whats wrong with my simple If Else?
提问by Jonah Katz
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)
我是 RoR/Ruby 的新手,我似乎无法让最简单的事情发挥作用。(相信我,我搜索谷歌并重新阅读文档,我不知道哪里出了问题)
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>
回答by apneadiving
Your statements are not intended to be displayed so instead of
您的陈述不打算显示,而不是
<%= if 1>2 %>
write
写
<% if 1 > 2 %>
Same thing for elseand end
同样的事情else和end
EDIT
编辑
<% if 1 > 2 %>
<%= "helllloooo" %> #option 1 to display dynamic data
<% else %>
nada #option 2 to display static data
<% end %>
回答by Dylan Markow
You don't need to use print, or even ERB for the text. Also, your if, else, and endstatements should be <%, not <%=:
您不需要print为文本使用, 甚至 ERB 。此外,您的if,else和end语句应该是<%,而不是<%=:
<% if 1 > 2 %>
helllloooo
<% else %>
nada
<% end %>
回答by Koraktor
<%=already means "print to the HTML response" in ERB (Ruby's own templating language).
<%=在 ERB(Ruby 自己的模板语言)中已经意味着“打印到 HTML 响应”。
So <%= print '...'means "print the return type of print '...'" which is nothing.
所以<%= print '...'意思是“打印打印'...'的返回类型”,这没什么。
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 %>
回答by Femaref
The =is the problem. Use <%instead. <%=is for printing something, while <%is for instructions.
这=就是问题所在。使用<%来代替。<%=用于打印某些东西,而<%用于说明。
回答by ankur
for dynamic content use: <%= %>
对于动态内容使用:<%= %>

