Question

Why is it that my index in a loop (in the object array bracket) is not working properly?

<% (1..7).each do |i| %>
  <%= select_tag 'hour[day][#{i}]', options_for_select(days_hours) %>
<% end %>

Shouldn't #{i} become 1, 2, 3, .... 7 in this loop?

<%= select_tag 'hour[day][1]', options_for_select(days_hours) %>
<%= select_tag 'hour[day][2]', options_for_select(days_hours) %>
<%= select_tag 'hour[day][3]', options_for_select(days_hours) %>
...
<%= select_tag 'hour[day][7]', options_for_select(days_hours) %>

When I check my HTML, it just stays #{i}.

Was it helpful?

Solution

Try using " " (double quotes) instead of ' ' (single quotes)

<% (1..7).each do |i| %>
  <%= select_tag "hour[day][#{i}]", options_for_select(days_hours) %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top