0

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?

1
  • @Sergio Tulentsev i tried to access the numbers and i can access each of them only by writing @array[0][0] for 1, array[1][0] for 2 previously i used to have my form like this <%= f.select(:connection_name, options_for_select(@array))%> but at that time my array had only numbers and so it was able to display all the numbers in the drop down – Commented Jul 12, 2012 at 22:02

2 Answers 2

1

Something like this would work:

@array.collect{|x| x[0]}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

The array is already present, so the idea is to not write out all the options by hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.