Rails obtiene el índice de" cada " bucle [duplicado]
Esta pregunta ya tiene una respuesta aquí:
- Encontrar el índice actual en CADA bucle (Ruby) [duplicado] 2 respuestas
- ¿Contador automático en Ruby para cada uno? 8 respuestas
Así que tengo este bucle:
<% @images.each do |page| %>
<% end %>
¿Cómo conseguiría el índice de" página " dentro del bucle?
180
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
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
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
Intenta each_with_index.
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
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