2014-02-14 58 views
0

我在他們的頁面上顯示客戶的資產,以及每個存在的3個資產,我想將它們包裝在<div class="row"></div>中。如何在Rails和Foundation中爲每3條記錄插入一個新的div?

現在我有這個在我看來:

<% if @assets.empty? %> 
    <h3>You don't have any uploaded images.</h3> 
<% else %> 
    <% for asset in @assets %> 
    <% assetcount += 1 %> 
    <div class="large-4 columns assetColumn text-center"> 
    <a href="#" class="asset-toolbar"><%= image_tag asset.file_name.url(:thumb).to_s %></a> 
    <div id="asset-toolbar-options<%= assetcount %>" style="display: none;"> 
    <a href="#" data-reveal-id="myModal<%= assetcount %>" data-reveal><span data-tooltip class="has-top tip-top radius" title="Enlarge Image"><i class="icon-zoom-in"></i></span></a> 
     <%= link_to [current_customer, asset], method: :delete do %> 
     <span data-tooltip class="has-top tip-top radius" title="Delete Image"><i class="icon-remove"></i></span> 
     <% end %> 
    </div> 
    <div id="myModal<%= assetcount %>" class="reveal-modal" data-reveal> 
    <%= image_tag asset.file_name.to_s %> 
    </div> 
    </div> 
    <% end %> 
<% end %> 

我嘗試使用split_each(3)但它完全搞砸了我的佈局。然後我試着用assetcount來跟蹤它是心靈爆炸:(

回答

2

可以使用Enumerable#each_slice

<% @assets.each_slice(3) do |assets_by_3| %> 
    <div class='row'> 
    <% assets_by_3.each |asset| %> 
     ... 
    <% end %> 
    </div> 
<% end %> 
0

我不知道爲什麼你正在使用assetcount。

可以使用紅寶石each_slice,所以你的觀點會是這樣的

<% if @assets.empty? %> 
    <h3>You don't have any uploaded images.</h3> 
<% else %> 
    <% @assets.each_slice(3) do |group_assets| %> 
    <div class="row"></div> 
    <% group_assets.each do |asset| %> 
    <div class="large-4 columns assetColumn text-center"> 
    <a href="#" class="asset-toolbar"><%= image_tag asset.file_name.url(:thumb).to_s %></a> 
    <div id="asset-toolbar-options<%= assetcount %>" style="display: none;"> 
    <a href="#" data-reveal-id="myModal<%= assetcount %>" data-reveal><span data-tooltip class="has-top tip-top radius" title="Enlarge Image"><i class="icon-zoom-in"></i></span></a> 
     <%= link_to [current_customer, asset], method: :delete do %> 
     <span data-tooltip class="has-top tip-top radius" title="Delete Image"><i class="icon-remove"></i></span> 
     <% end %> 
    </div> 
    <div id="myModal<%= assetcount %>" class="reveal-modal" data-reveal> 
    <%= image_tag asset.file_name.to_s %> 
    </div> 
    </div> 
    <% end %> 
<% end %> 
相關問題