如果然后声明查看

时间:2013-10-20 19:39:11

标签: ruby-on-rails ruby

如果读取列等于1,我正在尝试将索引视图置为“true”。

我尝试将此代码放在不同的地方,但没有结果:

if student.listening == 1 
  puts "Yes"
else
  puts "No"

这是github链接:https://github.com/keithgordon/classroom

1 个答案:

答案 0 :(得分:0)

如果您只想在视图中显示字符串,请执行以下操作:

 <% if student.listening == 1 %>
  Yes
 <% else %>
  No
 <% end %>

我想这样的事也应该有效:

<%= (student.listening == 1) ? 'Yes' : 'No' %>

[编辑] 在评论中,结果是'1'是一个字符串。所以正确的陈述是:

 <% if student.listening == "1" %>
  Yes
 <% else %>
  No
 <% end %>

因为“1”== 1不相等