Ruby on Rails迭代索引

时间:2016-04-13 08:36:38

标签: css ruby-on-rails ruby

以下是我向用户显示图像的视图:

<div id="lightgallery" class="col-md-8 detailpage-images-gallery" >
 <% if @product.product_attachments.present? &&@product.product_attachments.count > 0 %>
  <% @product.product_attachments.each_with_index do |attachment, index| %>

    <% if index == 0 %>
      <div class="col-sm-6 col-md-6 topleftimg <%='hidden-xs' if index !=  @product.product_attachments.count - 1 %>" data-responsive="<%= attachment.attachment.large.url %>" data-src="<%= attachment.attachment.large.url %>" data-sub-html="<%= @product.highlight %>">
        <a class="photo-grid-photo photo-trigger" style="background-image:url('<%= attachment.attachment.square.url %>');" href="#" title="Photo 2 of 6" data-index="1" data-prevent-default="true">
        <img src="<%= attachment.attachment.square.url %>" class="hide" alt="Photo 2 of 6"></a>
      </div>

    <% elsif index == 1 %>
     <div class="col-sm-6 col-md-6 toprightimg <%='hidden-xs' if index != @product.product_attachments.count - 1 %>" data-responsive="<%= attachment.attachment.large.url %>" data-src="<%= attachment.attachment.large.url %>" data-sub-html="<%= @product.highlight %>">                
      <a href=""><img src="<%= attachment.attachment.small.url %>" class="img-responsive"></a>
    </div>
    <% end %>                       
  <% end %>
 <% end %>
</div>

在我看来,我最多限制显示5张图片,但一旦用户点击,他们就可以看到超过5张图片。如何迭代index而不是使用<% if index == 0%>来获取所有图像?如何修改css,以便如果有超过5张图像,我可以将style="display:none"附加到div的其余部分?谢谢!

0 个答案:

没有答案