Flash and Ruby On Rails

written by seb on June 30th, 2006 @ 09:53 AM

I already seen more people looking at an article covering the communication of Flash with a server side language, in this case I will make an example with Ruby On Rails.

To be covered:

  • Basic application require for this article
  • Communication through XML file
  • Communication with loadVars (AJAX like let’s say)

During this article I will consider that you already have bases in Flash and Ruby On Rails.

Basic application require for this article

Let’s create a simple Ruby On Rails application running in local on *

Once this application is created we gonna add a *table “people” in database with the field id and name.

After that, let’s create a scaffold of it.

Now this is done, you should have running a scaffolder.

Add a entry in it and let’s finally approach the subject of this article, the communication with Flash.

Communication through XML file

Before opening Flash we gonna need one more things, create an XML page with the informations we would like to load in Flash. For this let’s change our app/controllers/people_controller.rb by adding the following action:

def list_xml
  render :xml => Person.find(:all).to_xml

Let’s check what we get: *

Great we got an XML file, just what we need for our first Flash communication example.

So now, let’s start Flash ;-)

Create a *new Flash Document, and on the first frame we gonna add the following actionscript code:

var my_xml:XML = new XML();
my_xml.ignoreWhite = true;
my_xml.onLoad = 
function(success:Boolean):Void  {
  if (success) {
  } else {
    trace("Error loading XML.");

Let’s run our Flash application and get our XML file in the output.

I agree that this code will not revolution the world, but would is enough to show how to load an external XML file in Flash, and more intersting, in our case, this XML file is brought up by Ruby On Rails.

Communication with loadVars (AJAX like let’s say)

Now let’s see an approach a bit more AJAX like if I can say.

In the Ruby On Rails application let’s make an other action (in app/controllers/people_controller.rb)

def list_flash
  render :xml => '&my_var_1='+params['my_var_1']+'&my_var_2='+params['my_var_2']+'&my_res=' + Person.find(:all).to_xml

If we get a look at it ( *) we gonna be surpise to get something not really nice, a crash!

Back in our *Flash Document let’s make the following actionscript code in the first frame:

var send_lv:LoadVars = new LoadVars();
var result_lv:LoadVars = new LoadVars();
result_lv.onLoad = function(success) {
  if (success) {
    trace(result_lv.my_var_1 + '\n---\n' + result_lv.my_var_2 + '\n---\n' + result_lv.my_res);
  } else {
    //notify of failure
send_lv.my_var_1 = 1;
send_lv.my_var_2 = "A string";
send_lv.sendAndLoad("", result_lv, "POST");

Let’s run it, and enjoy! That’s pretty cool, we got back from our Ruby On Rails application the 2 variables that we sent to it, and the list of people in XML format.

As you can see this example is really sketch and will need to be polish before real world use.

To remember:

A loadVars object is use to send variables to a server side application and an other one for receiving variables from the server side application.


That’s all for now, enjoy !

Comments are closed