ERB中的模运算符`each_with_index`

问题描述 投票:-1回答:1

each_with_index循环中的索引可以被4整除时,我想创建一个新的表行。即 - 我想要每行4个单元格。

当我尝试执行以下操作时,我得到undefined method %错误。

<% @brand_promotions.each_with_index do |index, brand_promotion| %>
        <% if (index % 4) == 0 %>
          <div class="row">
        <% end %>
        <div class="col-3">
          Column <%= index %>
        </div>
        <% if (index % 4) == 0 %>
          </div>
        <% end %>
<% end %>

我该如何实现这一目标?

ruby-on-rails erb
1个回答
0
投票

index是第二个参数

<% @brand_promotions.each_with_index do |brand_promotion, index| %>
© www.soinside.com 2019 - 2024. All rights reserved.