Rails obtiene el índice de" cada " bucle [duplicado]


Esta pregunta ya tiene una respuesta aquí:

Así que tengo este bucle:

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

<% end %>

¿Cómo conseguiría el índice de" página " dentro del bucle?

Author: criticerz, 2011-01-27

3 answers

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

<% end %>
 349
Author: PreciousBodilyFluids,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-27 00:12:07

Las dos respuestas son buenas. Y también te sugiero un método similar:

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

Es posible que no veas la diferencia entre esto y la respuesta aceptada. Permítanme dirigir sus ojos a estas llamadas de método: .each.with_index ver cómo es .each y luego .with_index.

 40
Author: Tatsuro Baba,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-27 00:13:39
 15
Author: Brett Bender,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-27 00:13:44