How to populate a dropdown in rails using a loop?

Viewed 1458

I would like to populate a dropdown in Rails dynamically using a loop. I want to display the number of days 1 to 30 in the dropdown. I tried this but it printed the select box multiple times on my html page.

  <%
    for i in 0..30
  %>
      <%= select_tag "time_id", options_for_select([['Last #{i} days', i]]), class: 'form-control' %>
  <%
    end
  %>

Output:

Last #{i} days got displayed 30 times within individual select boxes.

Expected Output:

<select>
    <option name='1'>Last 1 day</option>
    <option name='2'>Last 2 days</option>
    <option name='3'>Last 3 days</option>
    ...
    ...
    <option name='30'>Last 30 days</option>
</select>
1 Answers
Related