所以我有一些看起来像这样的 View 代码:
<div class="box-content">
<table class="table table-properties">
<tbody>
<%= render :partial => 'property', collection: @search.listings, as: :listing %>
</tbody>
</table>
</div>
在那
_property.html.erb
, 我有这个:<tr>
<td class="span2">
<%= link_to listing_path(listing), :class => "thumbnail thumb2" do %>
<%= image_tag "room_1.jpg", :alt => "Lucas" %>
<% end %>
</td>
<td>
<h2><%= link_to listing.headline, listing_path(listing) %></h2>
<h5><%= listing.listing_type.name if listing.listing_type "#{listing.neighborhood.name.capitalize}" %></h5>
<h5>Maintenance <%= number_to_currency(listing.maintenance) %></h5>
</td>
<td class="span1">
<h2 class="price"><%= number_to_currency(listing.price)%></h2>
<h5><%= "#{listing.num_bedrooms} bed, #{listing.num_bathrooms} bath" %></h5>
</td>
</tr>
基本上,我想为每一行准确地生成上面的代码,唯一的区别是我希望每第二行(即所有偶数行)都有
class=striped
..IE。 <tr class=striped>
.关于如何以 DRY 方式做到这一点的想法?
最佳答案
您是否尝试过使用 cycle
和 current_cycle
?
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-cycle
<tr class="<%= cycle('odd', 'even') -%>">
<!-- etc -->
</tr>
这将您的类(class)与
odd
交替和 even
并且恕我直言,在呈现集合时也应该工作。如果你需要多次实际循环的值,你可以用 current_cycle
得到它(因为多次调用 cycle
会弄乱你的周期,除非你想要那样)。
关于ruby-on-rails - 使用部分,如何以 DRY 方式呈现 `striped` 表行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14893079/