user1455116
user1455116

Reputation: 2144

ruby on rails drop down of array with f.select

I have an array like this:

@array = [[1, a], [2, b], [3, c] ............]

i want to create a drop down in my view with an

f.select 

the drop down should have only the numbers i.e., [1, 2, 3, ....]

How can i create a drop down with only the numbers and an f.select in my view?

Upvotes: 0

Views: 2158

Answers (2)

pratik
pratik

Reputation: 64

Just use. It is very easy to implement.

select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...])

or

f.select(:city_id, [['Lisbon', 1], ['Madrid', 2], ...])

Notice that the third parameter, the options array, is the same kind of argument you pass to options_for_select. One advantage here is that you don’t have to worry about pre-selecting the correct city if the user already has one — Rails will do this for you by reading from the @person.city_id attribute.

Upvotes: 0

mccarths
mccarths

Reputation: 83

Something like this would work:

@array.collect{|x| x[0]}

Upvotes: 1

Related Questions