ruby on rails如果在index.html.erb页面上带有boolean的语句

Rya*_*yan 12 ruby-on-rails

也许我错过了一些简单的东西,但是经过一段时间看这个我无法弄清楚.

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

我有这个

<% 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 %>
Run Code Online (Sandbox Code Playgroud)

但它回来了这个错误

编译错误

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"
Run Code Online (Sandbox Code Playgroud)

用箭头指着 .to_s

Chu*_*uck 31

首先,你测试与平等==,不=,这是赋值运算符.

第二 - 这就是错误所抱怨的 - 你需要使用plain <%而不是<%=if语句.后一种形式试图将其中的代码转换为字符串,当然写入(if something == true).to_s是没有意义的- 没有可能的字符串值.