AnthonyGalli.com AnthonyGalli.com - 7 months ago 9
Ruby Question

How to increment +1 in .times iteration?

<% @challenge.days_challenged.times do %>
<div class="text-background">
Day <%= + 1 %>
<%= render "notes/notes" %>
<%= render "notes/form" %>
</div>
<% end %>


Every note has "Day 1" at the top instead of "Day 1", "Day 2", "Day 3", etc depending on how many
@challenge.days_challenged

Answer

Use counter like so:

<% @challenge.days_challenged.times do |counter| %>
  <div class="text-background">
    Day <%= counter + 1 %>
    <%= render "notes/notes" %>
    <%= render "notes/form" %>
  </div>
<% end %>

Here, .times would pass the current step of the iteration starting from 0 to your block.