# Combobox from array

**URL:** <https://www.ruby-forum.com/t/combobox-from-array/190200>\
**Category:** Rails\
**Created:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/combobox-from-array/190200 "2010-06-17T16:22:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![maese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@maese](https://www.ruby-forum.com/u/maese)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/combobox-from-array/190200/1 "2010-06-17T16:22:18Z")

</div>

Hi, I’m new in ruby and i need do the next:

I want get a combobox from an array (@array) and save that value in a  
table called “form”, into a field called “combo”, i want save the  
value, not an id  
someone could tell me what i have to write?  
Thanks

---

<div class="post-metadata">

**Author:** ![maese](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@maese](https://www.ruby-forum.com/u/maese)\
**Post date:** [June 18, 2010, 5:43am UTC](https://www.ruby-forum.com/t/combobox-from-array/190200/2 "2010-06-18T05:43:25Z")

</div>

To get what you want you need two parts…

First you need an array of the objects in question and then you need a  
field on a form that uses that array.

To create the array you could do something like:

def create\_chapter\_list  
chapters = Chapter.find(:all)  
chapter\_list = Array.new  
chapters.each do |chapter|  
#Chapter ID must be place in the array as an integer - that is  
how it is stored in the DB  
chapter\_list \<\< [chapter.school\_and\_name, chapter.id]  
end  
return chapter\_list.sort!  
end

Note that the array has two values per element so its an array in an  
array. What you put in the first slot is what you would see in the  
list and the second is what you would have submitted in a form.

Then in the form you would reference that array:

\<%= f.select :chapter\_id, @chapter\_list, :prompt =\> ‘Select a  
Chapter’ %\>

In this example I am using a select object but the idea is the same  
for all the list selection types in a form. There are details on the  
form options in the RoR documentation … for example:

> **[ActionView::Helpers::FormOptionsHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M002302)**
>
> Provides a number of methods for turning different kinds of containers into a set of option tags.
