在rails中的数组之间插入。每次循环时

时间:2016-10-06 02:36:25

标签: ruby-on-rails

我有100个鞋子名字。我不想连续显示所有100个,我想要显示5,然后是绿色框,然后是下一个5,以及相同的绿色框...但我的代码有问题。

<% @shoes.each.with_index(1) do |shoe, index| %>
  <% while index < 101 do %>
    <%= shoe.name %>
    <% if index % 5 == 0 %>
      <%= Green Box %>
    <% end %>

2 个答案:

答案 0 :(得分:2)

您正在寻找C:\weblogic

in_groups_of(x)

答案 1 :(得分:0)

您的语法在多个地方都是错误的 - 枚举器错误,您缺少多个end语句。此外,即使index不是保留字,索引的普遍接受的样式也是单字母变量,如i。它应该是

<% @shoes.each_with_index(1) do |shoe, i| %>
  <% while i < 101 do %>
    <%= shoe.name %>
    <% if i % 5 == 0 %>
      <%= Green Box %>
    <% end %>
  <% end %>
<% end %>

(但就个人而言,我不会在视图中执行索引&lt; 101块 - 我会确保生成@shoes并将其发送到视图的控制器只发送数组中的100个元素)