[英]ruby on rails if statement with boolean on index.html.erb page
也許我錯過了一些簡單的東西,但是經過一段時間看這個我無法弄清楚。
我想檢查表單上的數據庫中是否為布爾值,如果它顯示向上箭頭,如果不是向下箭頭。
我有這個
<% 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
首先,使用==
,not =
測試相等性,這是賦值運算符。
第二 - 這就是錯誤所抱怨的 - 你需要使用簡單的<%
而不是<%=
使用if語句。 后一種形式試圖將其中的代碼轉換為字符串,當然寫入是沒有意義的(if something == true).to_s
- 沒有可能的字符串值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.