Spance
Spance

Reputation: 607

For loop with select helper

I have this form:

<%= form_for @score do |f| %>
    <%= f.hidden_field :site_id %> 
    <%= f.select(:value, [['1', 1], ['2', 2], ['3', 3], ['4', 4]]) %>       
    <%= f.submit "Submit" %>
<% end %>

I want to have the select values go all the way up to 10, but I don't want to manually put them in there.

Is there a way to DRY that line up? Or will have I have to manually put 10 elements inside that array?

Upvotes: 0

Views: 621

Answers (1)

tadman
tadman

Reputation: 211590

If they're just singular values, select can deal with that:

<%= f.select(:value, (1..10).to_a) %>

If you care about having string/number pairs:

<%= f.select(:value, (1..10).collect {|n| [ n.to_s, n ] }) %>

Upvotes: 2

Related Questions