Ruby-on-rails Rails 获取“每个”循环的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4811668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:12:41  来源:igfitidea点击:

Rails get index of "each" loop

ruby-on-railsruby

提问by criticerz

So I have this loop:

所以我有这个循环:

<% @images.each do |page| %>

<% end %>

How would I get the index of "page" inside of the loop?

我如何获得循环内“页面”的索引?

回答by PreciousBodilyFluids

<% @images.each_with_index do |page, index| %>

<% end %>

回答by Tatsuro Baba

The two answers are good. And I also suggest you a similar method:

两个答案都很好。我也建议你一个类似的方法:

<% @images.each.with_index do |page, index| %>
<% end %>

You might not see the difference between this and the accepted answer. Let me direct your eyes to these method calls: .each.with_indexsee how it's .eachand then .with_index.

您可能看不到此答案与已接受答案之间的区别。让我将您的目光引向这些方法调用:.each.with_index看看它是怎么回事.each,然后.with_index.

回答by Brett Bender