Trouble populating drop-down from DB

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 %>
            <%=
                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:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000399

c.

Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs