Getting XML into Flex from Ruby on Rails

Post to Twitter

This is going to be a simple tutorial to show you how easy it is to use Flex to read in XML from a Ruby on Rails application.

To proceed you will need to ensure you have the following installed:
1. Ruby
2. Ruby on Rails
3. Ensure SQLite is installed – here are the instructions for Windows Vista
4. A copy of Flex Builder 3

Ensure you are up and running with Ruby, Rails and Flex at this point. There are plenty of instructions on the web if you unsure how to set this all up.

The first thing we are going to do is create the Rails project. From my command prompt I create a folder called “Rails_Projects”. Navigate to that folder (or whatever your gonna call it). At command prompt enter the following:

Command Prompt

This command will then execute and you will see a bunch of data dumped to the screen as Rails generates the project. Once the project has been generated go ahead and move into the “SendXML” folder from the command prompt.

We now need to generate the databases using the following command (again, make sure you are in the “SendXML” folder):

Creating the databases

Now we have the databases created but no schema for the database. Lets add a very simple schema now:

Creating the schema

This is a pretty simple schema as we only have one column. You could of course make this much more complex pretty easily but for now we will stick with simple. Now we will actually have Rails create that schema by doing the following Rake task:

Running the DB:Schema Rake task

Now we need to actually add some data. Lets run the Rails project in order to do that. Again, from the command prompt:

Starting Webrick

Go to a browser and navigate to: http://localhost:3000/quotes

Now enter in a new quote.

Adding a new quote

While your at it, enter this into your browser to see the results in XML: http://localhost:3000/quotes.xml

The resulting XML

That takes care of the Rails part of this. Go ahead now an fire up Flex Builder 3 and create a new project. I called mine “TalkToRails”.

Creating the Flex Project

Here is the MXML:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="quoteService.send();" layout="absolute">

	<mx:Script>
		<![CDATA[

			private function onResult():void
			{
				trace(quoteService.lastResult.quote);
				// Yes, we could use data binding as well, I like coding though and don't
				// use the data binding features of Flex too much
				quoteOfTheDay_txt.text = quoteService.lastResult.quote["quote-of-the-day"];
			}

		]]>
	</mx:Script>

	<mx:HTTPService id="quoteService" result="onResult();" resultFormat="e4x" url="http://localhost:3000/quotes.xml" />

	<mx:Panel width="300" height="150" layout="absolute" horizontalCenter="0" verticalCenter="0" color="#000000" fontSize="14" fontFamily="Verdana" title="Quote of the day...">
		<mx:TextArea x="10" y="10" width="260" height="85" id="quoteOfTheDay_txt" verticalScrollPolicy="on"/>
	</mx:Panel>

</mx:Application>

You can see this is just a simple HTTP Service that calls the Rails application and requests the XML. Obviously if you returned more than one record you would need to modify this code. Also you could also do this same thing via data binding with much less code than I showed here.

The finished project

Post to Twitter

This entry was posted in ActionScript, Flex, Rails, Ruby. Bookmark the permalink.