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}
or
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(ResultEvent.RESULT,resFunc);
                service.addEventListener(FaultEvent.FAULT, failFunc);
               
          
            arg["account_id"] = accountId;
            arg["service_key"] = serviceKey;
           
            var req:Object = new Object();
            req["data"] = arg
           
            service.send(req);
           
        }



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




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

    http://cookbooks.adobe.com/post_Connecting_Flex_with_RoR_standard_scaffolding-18069.html

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

    ReplyDelete

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