# Trouble populating drop-down from DB

**URL:** <https://www.ruby-forum.com/t/trouble-populating-drop-down-from-db/76371>\
**Category:** Rails\
**Created:** [November 18, 2006, 1:05am UTC](https://www.ruby-forum.com/t/trouble-populating-drop-down-from-db/76371 "2006-11-18T01:05:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [November 18, 2006, 1:05am UTC](https://www.ruby-forum.com/t/trouble-populating-drop-down-from-db/76371/1 "2006-11-18T01:05:48Z")

</div>

Hello,

What I Have: I have a form that a user enters information in. That  
information is stored in a DB.

What I Want: I want to allow the user to edit/update that information  
-you know CRUD

My Problem: Everything is good until I try to populate the select list  
in the edit form. I do not know how to populate the list with the  
pre-defined options while having the user’s previous selection  
selected.

Code example or links to tutorial would be useful.

Thanks in advance – K

----The Code----

Model:

class Page \< ActiveRecord::Base  
belongs\_to :user

TEMPLATE\_STYLES = [

```
 ["History", "history"] ,
 ["Humanities", "human"],
 ["Life Sciences", "life"],
 ["Engineering", "eng"],
 ["Social Sciences", "social"]

```

]

validates\_presence\_of :file, :title, :template  
#…

Controller:  
class AdminController \< ApplicationController

def create\_page  
@page = Page.new(params[:page])  
@page.user = User.find\_by\_id(session[:user\_id])  
if request.post? and @page.save  
flash.now[:notice] = “Page #{@page.title} was created”  
@page = Page.new  
redirect\_to( :action =\> ‘index’)  
end  
end

def edit  
@page = Page.find(params[:id])  
end

def update  
@page = Page.find(params[:id])  
if @page.update\_attributes(params[:page])  
flash[:notice] = ‘Page was successfully updated.’  
redirect\_to :action =\> ‘list\_pages’  
else  
render :action =\> ‘edit’  
end  
end

View:  
\<%= start\_form\_tag :action =\> ‘update’, :id =\> @page %\>

```
        <p>
            <label for="page_title" >Page Title:</label>
            <%= text_field 'page', 'title' %></p>
        </p>

        <p>
            <label for="file_name" >File Name:</label>
          <%= text_field 'page', 'file' %></p>
        </p>
        <p>
            <label for="template" >Template:</label>
            <%=
                form.select :template,
                Page::TEMPLATE_STYLES,
                ?????
            %>

        </p>
        <%= submit_tag "Edit Page" , :class => "submit" %>
   <%= end_form_tag %>
```

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [November 18, 2006, 3:31am UTC](https://www.ruby-forum.com/t/trouble-populating-drop-down-from-db/76371/2 "2006-11-18T03:31:02Z")

</div>

> ```
> <%=
> form.select :template,
> Page::TEMPLATE_STYLES,
> ?????
> %>
> 
> ```

\<%= select(“page”,“template”,Page::TEMPLATE\_STYLES, {}, {:class =\>  
“class”}) %\>

Should give you a drop-down select with the current value of  
@page.template selected.

The first set of parms in {} are options to the select helper. The  
second set are HTML options to the resulting tag, like the  
class attribute that I set in the example. Both of these sets of parms  
are optional and can be left off, but if you’re going to put anything in  
the second one, you have to at least include the first one empty, as  
above.

See following for detailed doc:

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

c.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [November 21, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/trouble-populating-drop-down-from-db/76371/3 "2006-11-21T20:46:46Z")

</div>

Thanks.
