Rails 如何使用 in_groups_of 建立索引

use*_*963 1 ruby-on-rails

我正在使用 in_groups_of 创建 3 行的视图中显示图片。但是,我需要对第一行的第一项做一些不同的事情。如何定位此项目?

<% images.in_groups_of(3) do |row| %>
  <div class="row">
    <% row.each_with_index do |image, index| %>
      <div class="item-wrapper">
        <% if image %>
          <a class="flex-item" <img class="photo" src="<%= image.picture.medium.url %>" alt="" /></a>
        <% end %>
      </div>
    <% end %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

除了第一行中的第一张照片之外,我想在每个锚标记下方添加此代码。

<% if user.eql?(current_user) %>
  <button class="btn btn-xs btn-danger delete-image" data-id="<%= image.id %>">
    <i class="fa fa-trash-o fa-lg"></i>
  </button>            
<% end %>
Run Code Online (Sandbox Code Playgroud)

oll*_*lpu 6

要获取当前行的索引,请尝试以下操作:

<% images.in_groups_of(3).each_with_index do |row, row_index| %>
Run Code Online (Sandbox Code Playgroud)

现在您可以检查两者都不是row_indexindex为零,然后打印所需的代码。