我按日期创建多个表格,并且希望我的html表格行根据多种条件是不同的颜色。随着this question的帮助下,我尝试了,我转变观点如下:在Rails条件中更改表格行颜色
<% (Date.current - 6.months..Date.current + 6.months).each do |date|%>
<% if !Shift.where(date: date).blank? %>
<% @shifts = LaunchShift.where(date: date).all.order(start_time: :asc, booked: :desc) %>
<h2><%= date.strftime('%e %b %Y') %></h2>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>...</th>
</tr>
</thead>
<% @shifts.each_with_index do |shift, index| %>
<% if shift.booked? %>
<% @booker = Member.find(shift.booked_by) %>
<% end %>
<tbody>
<% if shift.booked? %> <---Trying this for changing colour
<tr style="background-color:red">
<% else %>
<tr>
<% end %>
<td><%= shift.start_time.strftime("%H:%M") %></td>
<% if shift.booked? %>
<td>Booked</td>
<td><%= link_to @booker.name, member_path(@booker) %></td>
<% else %>
<td>Free</td>
<td></td>
<% end %>
[...]
</tr>
</tbody>
<% end %>
</table>
</div>
<% end %>
但只有部分行标为预订的是红色的,虽然shifts.booked?返回为True。这种方法的工作方式与我认为的不同吗?有没有更好的方式来做到这一点,不使用JS/JQuery(不知道这些)。有什么建议? 谢谢!
谢谢你,这是一个更好的办法。但问题仍然是逻辑在某处出错。并非所有预订的班次都是红色,并非所有可用的班次都是灰色的这可能与创建表的循环内的循环有关吗?数据正确显示,但不知何故if语句不总是工作。有任何想法吗? – SKR
如果陈述不起作用意味着什么? – ashvin
@SKR如果数据显示正确,那么如果条件应该是正确的, 想法是,如果班次被预订,那么红色将适用,否则灰色将适用于