我有这样一个循环:
<% @images.each do |page| %>
<% end %>
我如何得到循环内的“页”的索引?
我有这样一个循环:
<% @images.each do |page| %>
<% end %>
我如何得到循环内的“页”的索引?
当前回答
<% @images.each_with_index do |page, index| %>
<% end %>
其他回答
<% @images.each_with_index do |page, index| %>
<% end %>
each_with_index试试。
这两个答案都很好。 我还建议你一个类似的方法:
<% @images.each.with_index do |page, index| %>
<% end %>
你可能看不出这和公认答案之间的区别。让我把你的眼睛引向这些方法调用:.each。With_index是。each和。With_index。