0 Replies Latest reply on Mar 27, 2008 4:36 PM by mattcom

    Populating <mx:ComboBox with database info

    mattcom Level 1
      Ok, so I set out on a quest to populate <mx:ComboBox with info from a database using Flex and Ruby on Rails. I had a really hard time finding tutorials about this subject, but using information from a large set of tutorials and tweaking the code over and over, I finally figured out something that works.

      My model is sourcer
      My controller is sourcers and contains the followign code:
      ------------------------------------------------------------------------
      class SourcersController < ApplicationController
      def create
      @sourcer = Sourcer.new(params[:sourcer])
      @sourcer.save
      render :xml => @sourcer.to_xml
      end

      def list
      @sourcer = Sourcer.find :all
      render :xml => @sourcer.to_xml
      end

      def update
      @sourcer = Sourcer.find(params[:id])
      @sourcer.update_attributes(params[:sourcer])
      render :xml => @sourcer.to_xml
      end

      def delete
      @sourcer = Sourcer.find(params[:id])
      @sourcer.destroy
      render :xml => @sourcer.to_xml
      end
      end
      ----------------------------------------------------------------
      My migration file had this code:
      ------------------------------------------------------------------
      class CreateSourcers < ActiveRecord::Migration
      def self.up
      create_table :sourcers do |t|
      t.column :sourcername, :string
      t.timestamps
      end
      end

      def self.down
      drop_table :sourcers
      end
      end
      ---------------------------------------------
      and finally the application code contained:
      ---------------------------------------------
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" >
      <mx:HTTPService id="listSourcers" url=" http://localhost/sourcers/list" useProxy="false" method="GET"/>
      <mx:VDividedBox x="0" y="0" height="100%" width="100%">
      <mx:Panel width="100%" height="300" layout="vertical" title="Create/Update Denial Reasons">
      <mx:Form>
      <mx:FormItem>
      <mx:ComboBox dataProvider="{listSourcers.lastResult.sourcers.sourcer}" labelField="sourcername" creationComplete="listSourcers.send()"/>
      </mx:FormItem>
      </mx:Form>
      </mx:Panel>
      </mx:VDividedBox>
      --------------------------------------------------------------
      Now this can be condensed to the following important items:
      ---------------------------------------
      <mx:HTTPService id="listSourcers" url=" http://localhost/sourcers/list" useProxy="false" method="GET"/>
      <mx:ComboBox dataProvider="{listSourcers.lastResult.sourcers.sourcer}" labelField="sourcername" creationComplete="listSourcers.send()"/>
      ---------------------------------------
      Things to note -
      - if you leave out the labelField="sourcername" in the <mx:ComboBox> tag you will "object: Object" in the dropdown after compile.
      - if you leave out the creationComplete="listSourcers.send()" in the <mx:ComboBox> you will get a dropdown that is blank, but is sized correctly. Also something to note on this line is it can be included in the <mx:Application> tag but then if you have multiple ComboBoxes you're going to need it in each of the <mx:ComboBox> tags.


      Anyway, I hope this helps someone.