开发者

Rails get index of "each" loop [duplicate]

开发者 https://www.devze.com 2023-02-06 18:56 出处:网络
This question already has answers here: Finding out current index in EACH loop (Ruby) [duplicate] (2 answers)
This question already has answers here: Finding out current index in EACH loop (Ruby) [duplicate] (2 answers) Automatic counter in Ruby for each? (8 answers) Closed 6 years ago.

So I have this loop:

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

<% end %>

How would I get the index 开发者_如何学Cof "page" inside of the loop?


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

<% end %>


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_index see how it's .each and then .with_index.


Try each_with_index.

0

精彩评论

暂无评论...
验证码 换一张
取 消