Tuesday, August 2, 2011

Flex and Rails

Hi Peter
Thanks for visiting my blog

In my company we have quite an extensive use of the ruby - flex combination, and to my experience it has the best results for the fastest development time.

The key to communicate between rails and flex is to set up REST services that generate json/xml structures.

On the server side, all you have to do is have the controller render the result as json/xml using the following line:

render :json => {an object with the data you want to send}
render :xml => {an object with the data you want to send}

It works just as well in rails 3 as in 2.x

On the flex side, it gets a little more complicated, but in essence, you send an http request to the rails service and on the ResultEvent you encode the string you get to json/xml respectively

here is a code example:

protected function call(action:String,arg:Object,resFunc:Function, failFunc:Function,resultFromat:String="e4x"):void{
            var service:HTTPService;
                service = new HTTPService();
                service.url = server+"/"+action;
                service.method = "POST";
                service.contentType = "application/xml";
                service.resultFormat = resultFromat;
                service.useProxy = false;
                service.addEventListener(FaultEvent.FAULT, failFunc);
            arg["account_id"] = accountId;
            arg["service_key"] = serviceKey;
            var req:Object = new Object();
            req["data"] = arg

public function getDetails(result:Function,fault:Function):void{
            var o:Object = new Object();

private function detailsResult(e:ResultEvent):void{
                details= JSON.decode(String(e.result));   

1 comment:

  1. Have been working through this and loving your examples etc


    The ServiceTester.mxml is missing. Can you please repost or send an example of .mxml code that is using the new service in flex?


Please do not post spam on this blog, Spam sites will be reported to google.
thank you kindly.