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 %>