我要求制作一个有视图的应用。在该视图中,我需要检查一个条件,如果它是真的,那么需要适当地为表格行着色。最简单的方法是在视图中使用不同的标题。但是,如果我想将所有样式信息保存在CSS中,该怎么做?
答案 0 :(得分:3)
如果它只是你想要着色的一行,那么你可以在视图中执行它,不需要乱用标题:
<tr class="<%= "blue" if some_condition %>">
<td>Your text</td>
</tr>
或者:
<% if some_condition %>
<tr class="blue">
<% else %>
<tr class="red">
<% end %>
<td>Your text</td>
</tr>
答案 1 :(得分:0)
application_helper.rb
def my_color_for(condition)
if condition
'white'
else
'blue'
end
end
view.haml
- @array.each do |a|
%tr{class: my_color_for(a.value)}