【问题标题】:ruby on rails if statement with boolean on index.html.erb pageruby on rails if 语句在 index.html.erb 页面上带有布尔值
【发布时间】:2009-07-29 15:36:44
【问题描述】:

也许我错过了一些简单的东西,但看了一段时间后我无法弄清楚。

我想检查表单上数据库中的布尔值是否为真,是否显示向上箭头,如果不是向下箭头。

我有这个

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

但是这个错误又回来了

编译错误

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"

箭头指向.to_s

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    首先,您使用== 测试是否相等,而不是=,它是赋值运算符。

    第二——这就是错误所抱怨的——你需要使用简单的&lt;%而不是&lt;%=和if语句。后一种形式试图把里面的代码变成一个字符串,当然写(if something == true).to_s是没有意义的——没有可能的字符串值。

    【讨论】:

    • 谢谢,我知道这一定很简单。我的大脑今天不能正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多