![](/img/trans.png)
[英]Using simple_form, how do I render a 'new' form on the view of another controller in a DRY way?
[英]Using partials, how do I render `striped` table rows in a DRY way?
所以我有一些看起來像這樣的視圖代碼:
<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>
。
關於如何以干燥的方式做到這一點的想法?
你嘗試過使用cycle
和current_cycle
嗎?
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-cycle
<tr class="<%= cycle('odd', 'even') -%>">
<!-- etc -->
</tr>
這會使您的班級與odd
和even
交替,恕我直言也應該在渲染集合時起作用。 如果你需要多次實際循環的值,你可以用current_cycle
得到它(因為多次調用cycle
會擾亂你的循環,除非你想要那樣)。
使用:nth-child()
css選擇器不是更好嗎? http://www.w3schools.com/cssref/sel_nth-child.asp
你可以嘗試這樣的事情:
<tr class="<%=cycle("odd", "even") %>">
<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>
用jquery做:
$(document).ready(function(){
$("table.table-properties > tbody > tr:odd").addClass("odd");
$("table.table-properties > tbody > tr:not(.odd)").addClass("even");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.