ruby on rails if 语句在 index.html.erb 页面上带有布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1201169/
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
ruby on rails if statement with boolean on index.html.erb page
提问by Ryan
Maybe I'm missing something simple but I can't figure out after some time looking at this.
也许我错过了一些简单的东西,但看了一段时间后我无法弄清楚。
I want to check if a boolean is true in a database on a form, if it is display a up arrow, if not down arrow.
我想检查表单上数据库中的布尔值是否为真,是否显示向上箭头,如果不是向下箭头。
I have this
我有这个
<% for probe in @probes %>
<tr id="<%= cycle('list-line-odd', 'list-line-even') %>">
<td>
<%= if probe.online = true %>
<%= image_tag("online-icon.png", :alt => "Online") %>
<%= end %>
</td>
<td><%= link_to probe.probe_name, probe %></td>
</tr>
<% end %>
but it's coming back this this error
但它又回来了这个错误
compile error
编译错误
syntax error, unexpected ')', expecting kTHEN or ':' or '\n' or ';'
@output_buffer.concat "\t \t"; @output_buffer.concat(( if probe.online = true ).to_s);
@output_buffer.concat "\n"
syntax error, unexpected kEND
@output_buffer.concat " \t \t"; @output_buffer.concat(( end ).to_s);@output_buffer.concat "\n"
with arrows pointing at the .to_s
箭头指向 .to_s
回答by Chuck
Firstly, you test for equality with ==, not =, which is the assignment operator.
首先,您测试与==, not 的相等性=,它是赋值运算符。
Second — and this is what the error is complaining about — you need to use just plain <%rather than <%=with an if-statement. The latter form tries to turn the code inside it into a string, and of course it's meaningless to write (if something == true).to_s— there's no possible string value for that.
其次——这就是错误所抱怨的——你需要使用简单的<%而不是<%=if 语句。后一种形式试图将其中的代码转换为字符串,当然写它是没有意义的(if something == true).to_s——没有可能的字符串值。

