onrails.org home

RESTFul Rails from Flex.

As part of the “MySpyder” project I am currently working on we want a Flex front-end to access a RESTFul Ruby on Rails service. You can read more on RESTFul and ActiveResource on David’s blog, on the “release notes” of Rails 1.2, PeepCode as an excellent screencast (not free) on the subject, and many other places.


In short using the RESTFul approach allows to expose and manipulate a Rails model via a predefined set of standard Http requests. For example we have an ActiveRecord named Watch which can be manipulated via the following requests:

command url
index # GET /watches.xml
show # GET /watches/1.xml
new # GET /watches/new
create # POST /watches.xml
update # PUT /watches/1.xml
delete # DELETE /watches/1.xml

This allows for standard CRUD operations. Note rest supports also custom operations and CRUD operations on nested resources (such as a has_many relationship). We won’t address them in the article, but I will certainly need them later in the project.


The Rails application can determined what format to return based on the content type or the extension of the url. We are only interested in xml for the moment.


Some of the advantages of a RESTFul Rails application are that it provides a standard way to organize your controllers, as you will see just in a moment because the controllers are standard most of the code can easily be generated, and because the URL to access the application are standard we can now provide some standard utility class to access a RESTFul server. Rails provides an ActiveResource client class, and I haven’t found one yet for Flex. So I started to write one which I will show here after. Now please contact me or add some comments to this blog if you find anything similar or if you want to help me out writing this class. Another cool feature of writing RESTFul Rails controllers is that you get an API for your application nearly for free. This is the main reason we went down that direction.


Assuming we have an ActiveRecord named Watch we can now generate a RESTFul Ruby on Rails controller issuing the following command

./script/generate scaffold_resource watch

Our server now supports RESTFul http requests.


Now wouldn’t it be nice if we could access the server data from Flex in the following manner:

import mx.rpc.AsyncToken;
import org.onrails.ActiveResourceClient;

var watches:ActiveResourceClient = new ActiveResourceClient();
watches.defineResource(“http://localhost:3000”, “watches”, resultHandler, faultHandler);

// Note that all the calls are performed in parallel and asynchronously.
watches.list()
watches.show(8)

// create
var data:XML =
www.picnik.com

10080

watches.create(data)

// update
data =
8
10080

data.frequency = 1440;
watches.update(data)

// delete
watches.deleteItem(8)

Now we can. I started to write the ActiveResourceClient actionscript class to allow this. It’s attached at the end of the article. Now, this is a first try I’ve created this morning. So please be critical and let’s improve it together, or even better if you have one or know one that’s way better please point me where I can get it.


To use the ActiveResourceClient just put the ActiveResourceClient.mxml file into the org.onrails folder in your Flex project. Note that you can provide a default result handler and fault handler or set one handler for each specific call, i.e. the watches.create returns an AsyncToken where a dedicated handler can be specified just to handle the result of the creation.


Download: ActiveResourceClient.mxml (renamed file to ActiveResourceClient.mxml)


Enjoy,
Daniel Wanja.

Fork me on GitHub