SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Cape Town
    0 Post(s)
    0 Thread(s)

    Populating select list in form

    Just finished the Agile Web Development with Rails book, and am working on my first little app...

    I have a form that allows one to create new tasks. But a task can have (an optional) parent task, so I need a select list in the form which is populated with all the existing tasks.

    My task table looks like this:
    CREATE TABLE `tasks` (
      `id` int(4) NOT NULL auto_increment,
      `parent_id` int(4) default NULL,
      `title` varchar(100) NOT NULL default '',
      `description` text NOT NULL,
      `estimated_hours` int(4) NOT NULL default '0',
      `start_on` date default NULL,
      `due_on` date NOT NULL default '0000-00-00',
      `user_id` int(4) NOT NULL default '0',
      `client_id` int(4) NOT NULL default '0',
      `type_id` int(4) NOT NULL default '0',
      `urgent` tinyint(4) default NULL,
      `on_hold` tinyint(4) default NULL,
      `percent_complete` int(4) NOT NULL default '0',
      PRIMARY KEY  (`id`),
      KEY `parent_id` (`parent_id`),
      KEY `client_id` (`client_id`),
      KEY `type_id` (`type_id`),
      KEY `user_id` (`user_id`)
    I think I've got all the relationships set up properly in my models, its just this form that's confusing me. You'd think you'd find lots of examples of this kind of thing, but even my recently finished book doesn't give any examples of populating a select from a db.

    Can anyone help me with this, or even point me to a site with some good examples.


  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Worcs. UK
    0 Post(s)
    0 Thread(s)
    I too am on my first "live" Rails application. I think I am doing the same sort of thing you are. I have a set of contracts and want to tie them to a list of customers. To allow me to create a list of customers as a select list in the contract submission form, I first needed to define an object containing an array of customer objects populated from my customers table.

    @customers = Customer.find_all

    This I did in the contract controller, "new" action definition. With that object defined and populated via the controller, I was then able to use the following code to generate a select list populated from the @customers object in the "new" view new.rhtml.
    <%= collection_select("contract", "customer_id", @customers, :id, :name) %>
    That uses the @collection object, defines the parameter returned by the form as a customer_id part of contract. Sets the as the value, and the as the text that appears in the select list.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts