I got Flash to talk to Rails

I went through this tutorial, and replaced all the Flex stuff with 56 lines of ActionScript in Flash 8. Getting XML into Flash is the Flash Developer’s tried, true, and trusted way of getting dynamic data. Took me 30 minutes in total: 30 seconds to do the rails setup, 20 minutes to install/uninstall/reinstall MySQL 4.1 (POS!!!), and 5 to get data. I spent another 15 unsucessfully sending XML to the create method. It was creating records, but wasn’t getting the XML I sent. I tried every version of the XML object I know to no avail.

Here, I created the XML string by hand:

function createUser()
{
	trace("createUser");
	var s:String = "";
	s += "<request>";
	s += "<user>";
	s += "<updated-date/>";
	s += "<creation-date/>";
	s += "<username>doom</username>";
	s += "<id type='integer'></id>";
	s += "<password>heck</password>";
	s += "<email>doom@finalbattle.com</email>";
	s += "</user>";
	s += "</request>";

And then here I send the request:

create_xml = new XML();
create_xml.ignoreWhite = true;
create_xml.parseXML(s);
trace("---------------");
trace("sending: create_xml: " + create_xml);
create_xml.contentType = "application/xml";
create_xml.onLoad = function(success)
{
        trace("create success: " + success);
        trace(this);
};
create_xml.sendAndLoad("http://localhost:3000/users/create", create_xml, "POST");
}

I opened ServiceCapture and started examining the traffic, and nothing jumped out at me. I got pulled away to do a Podcast so I’ll just have to do it in Flex, and compare since I’m probably just not formulating my XML message correctly.

Anyway, damn yo… 5 minutes to read from and write to a DB? Awesome!

9 Replies to “I got Flash to talk to Rails”

  1. Flash and rails work very well together… have a look at builder for serialising xml, we use rails as our weapon of choice now and it works like a charm (think of flash as the view) and with your application running in production mode (rails sets to development as default) watch that mutha fly :)

    Creating XML with Ruby and Builder

    Builder

  2. J3s73r,

    Although the request is created from xml in the flex example, the flex framework translates such an xml structure in a vanilla http request (GET or POST)

    so chuck the xml in your example and just whip a up a vanilla post shake. Remember to follow the RoR way to create form field names (just take a look at the generated scaffold). probably something like ‘user[username]’

    ruby on rails and flex rock soo hard, it wil hurt your ears.

  3. Hrm… examining the traffic, Flex and Flash are sending the same thing; XML encoded strings (content type of ‘application/xml’). Got ’em both working (Flash 8 and Flex 2 beta 3), so I just was probably sending the XML wrong. Cool!

  4. Hi Jesse,

    I was just going to say that the node is not needed. The node should be the root, it is this that Rails will interupt to params[:user].

    Glad you liked the tutorial.

    Stuart

  5. i’m playing around with rails and flash also. Where is the best place to put your swf?

    I can kind of get the swf functioning and displaying by putting it in the public folder. But isn’t the rails philosphy to put the swf in the views folder?

Comments are closed.