假设我有一个这样的表:
我想根据类型 {id1: 'Anamnse', id2: 'Befund', id3: 'Therapie'} 设置表格行的样式。手动我会简单地添加 Bootstrap 类
我的问题是如何使用 ruby 自动实现这一点?这是我的代码,用于表格:
<table id="report" class="table table-striped ">
<tr>
<th>Typ</th>
<th>Beschreibung</th>
<th>Datum</th>
<th> </th>
</tr>
<% @patient.treatments.each do |treatment| %>
<tr>
<td><%= treatment.category.try(:typ) %></td>
<td><%= treatment.content %></td>
<td><%= treatment.day %></td>
<td><div class="arrow"></div></td>
</tr>
<tr>
<td colspan="5">
<%= link_to 'Löschen', [treatment.patient, treatment],
:confirm => 'Sind sie sicher?',
:method => :delete %>
<% treatment.paintings.each do |paint| %>
<%= image_tag paint.name.url(:thumb) %>
<% end %>
</td>
</tr>
<% end %>
</table>
结论:样式必须依赖于
<td><%= treatment.category.try(:typ) %></td>
请您参考如下方法:
你也可以这样使用辅助方法
<td <%= row_classname(:typ) %> ><%= treatment.category.try(:typ) %></td>
和你相关的助手
def row_classname(type)
switch type
when "somevalue"
classname = "someclass"
when "othervalue"
classname = "otherclass"
"class=\"#{classname}\"" unless classname.nil?
end
或类似的东西






