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

How to match date with counter?

I'd like to show for example:

Day 1 with Sun, 24 April 2016
Day 2 with Sun, 25 April 2016
Day 3 with Sun, 26 April 2016
etc...


All the dates however are being listed under each
days_challenged
.

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


enter image description here

Answer

You've got to loop them both together. For example:

<% @challenge.dates_challenged.first(@challenge.days_challenged).each_with_index do |date, i| %>
  <div class="text-background">
     Day <%= i + 1 %>
     <%= date %>
     <%= render "notes/notes" %>
     <%= render "notes/form" %>
   </div>
<% end %>

That will take up to as many 'days challenged' entries and iterate over them.

Comments