# Populating drop down

**URL:** <https://www.ruby-forum.com/t/populating-drop-down/124591>\
**Category:** Rails\
**Created:** [December 30, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/populating-drop-down/124591 "2007-12-30T18:28:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ochomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@ochomo](https://www.ruby-forum.com/u/ochomo)\
**Post date:** [December 30, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/populating-drop-down/124591/1 "2007-12-30T18:28:12Z")

</div>

hi all,  
i need help in populating a combo box with table data.  
any sample code out there?

ochomo

---

<div class="post-metadata">

**Author:** ![ochomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@ochomo](https://www.ruby-forum.com/u/ochomo)\
**Post date:** [December 30, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/populating-drop-down/124591/2 "2007-12-30T18:53:03Z")

</div>

most probably you want to use the collection\_select helper in your  
view

first, in your controller action, grab the contents of the table you  
want to use to populate the combo

e.g.

class TestController \< ApplicationController

def index  
@categories = Category.find(:all)  
end

end

Then in your view, add the following code to insert a form and combo  
 box

\<% form\_for :articles do |f| %\>  
\<%= f.collection\_select(:category\_id, @categories, :id, :name) %\>  
\<% end %\>

This is assuming you have a table in your db named categories which  
has an ‘id’ and ‘name’ column, and you want to save the id into the  
column ‘category\_id’ in the table ‘articles’

Just substitute out these names if yours are different.

I find this is the best place to read up on the myriad helpers and  
methods available in rails: [http://www.gotapi.com/rubyrails](http://www.gotapi.com/rubyrails)

On Dec 30, 5:28 pm, Zablon O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ochomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/45deac/32.png) [@ochomo](https://www.ruby-forum.com/u/ochomo)\
**Post date:** [December 30, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/populating-drop-down/124591/3 "2007-12-30T20:42:24Z")

</div>

thanks. i think i luck elaborate ref material.
