0

我有一个名为“Photo”的模型,它属于一个名为“Shoe”的模型。我使用 Carrierwave 上传多张图片。

index.html.erb

  <% shoes.each do |shoe| %> 
    <div class="shoe">
      <div class="gallery">
        <% shoe.photos.each do |photo| %>
          <%= link_to image_tag(photo.photo_file.url(:thumb).to_s), photo.photo_file.url.to_s, :class => 'fancybox', :rel => 'gallery' %>
        <% end %>
      </div>
    </div>
  <% end %>

输出这个:

<div class="shoe">
     <div class="gallery">
        <a class="fancybox" href="../nike-kd-6-meteorology-2.jpg" rel="gallery">
           <img src="../thumb_nike-kd-6-meteorology-2.jpg">
        </a>
        <a class="fancybox" href="../nike-kd-6-meteorology-2.jpg" rel="gallery">
           <img src="../thumb_nike-kd-6-meteorology-2.jpg">
        </a>
        <a class="fancybox" href="../nike-kd-6-meteorology-2.jpg" rel="gallery">
           <img src="../thumb_nike-kd-6-meteorology-2.jpg">
        </a>
     </div>
</div>

但我希望每个画廊的第一个图像能够拥有自己的类,而其余图像拥有自己的类。像这样的东西:

        <a class="firstclass" href="../nike-kd-6-meteorology-2.jpg" rel="gallery">
           <img src="../thumb_nike-kd-6-meteorology-2.jpg">
        </a>
        <a class="fancybox" href="../nike-kd-6-meteorology-2.jpg" rel="gallery">
           <img src="../thumb_nike-kd-6-meteorology-2.jpg">
        </a>
        <a class="fancybox" href="../nike-kd-6-meteorology-2.jpg" rel="gallery">
           <img src="../thumb_nike-kd-6-meteorology-2.jpg">
        </a>

我怎样才能做到这一点?

另外我希望每个画廊都有自己唯一的 id,但是当我尝试添加这个时:

:rel => 'gallery<%= shoe.id %>'

我收到语法错误。

谢谢。

4

1 回答 1

1

像这样的东西?

<% shoes.each do |shoe| %> 
  <div class="shoe">
    <div class="gallery">
      <% shoe.photos.each_with_index do |photo, index| %>
        <% image_class = index.zero? ? "firstclass" : "fancybox" %>
        <%= link_to image_tag(photo.photo_file.url(:thumb).to_s), photo.photo_file.url.to_s, :class => image_class, :rel => "gallery_#{shoe.id}" %>
      <% end %>
    </div>
  </div>
<% end %>
于 2013-06-24T22:19:35.570 回答