Ruby on Rails
Flex On Ruby on Rails
Automating the communication between the client and server
By: Harris Reynolds
Nov. 20, 2006 12:00 PM
With the release of Flex 2, Adobe has introduced a tool that makes building rich user interfaces for the Web easier than ever; of course, as anyone remotely plugged in to the Web development community knows, Ruby on Rails has made creating database-driven Web applications brain dead simple. This article discusses a technology that marries Flex with RoR applications by providing a means of automating the communication between the client and server. The technology is "WebORB for Rails," a free and open source (GPL) server made available by Midnight Coders (www.themidnightcoders.com).
Many of the tutorials on the Web that talk about integrating Flex clients with Rails discuss using raw XML over HTTP. While this mechanism is nice for the standard "mom and apple pie" tutorial that return a simple list of records, this level of integration quickly becomes unwieldy as the complexity of the application grows. With each new operation on the server, a developer must spend valuable cycles serializing and deserializing requests and responses before they can focus on the actual business logic.
WebORB relieves the burden of this "serialization tax" by supporting the concept of remote objects whereby Flex clients can natively invoke methods on the server and retrieve the responses all within ActionScript, the object-oriented programming language used by Flex.
This article will discuss installing WebORB to "Flex-enable" a Ruby on Rails application, the steps required to create an application using Flex + WebORB + RoR, and some advanced topics like dealing with Rails models, etc.
Installing WebORB for Rails
./script/plugin install http://themidnightcoders.net:8089/svn/weborb/
or on Windows:
ruby script/plugin install
Writing an Application Using WebORB
This example is bundled within the WebORB on Rails product and simply returns a bag of properties related to the server running the service. Generally, the classes that are surfaced as remote objects are added to a /services directory under the RAILS_APP/app directory.
After creating this class, you must also tell Flex that this class needs to be available as a remote object. This is configured by adding the following snippet of XML to the remoting-config.xml file located within the RAILS_APP/config/WEB-INF/flex directory:
Last, this service is invoked by a client by using the RemoteObject class within ActionScript. Below is a small chunk of code that demonstrates how to use this class:
remoteObject = new RemoteObject();
Assuming that the remote object variable was declared at the top of your MXML application, this code illustrates creating and initializing a remote object for use. Note in particular that the destination property is set to "InfoService", which maps directly to the ID attribute of the destination we just configured within remoting-config.xml. Note that this is how Flex (and WebORB) determines where to send this request. The final two lines above demonstrate adding callback methods to the remote object; the first line tells Flex to pass the result of a call to the getComputerInfo method (this maps to the Rails service method on the server) to the onResult method (which is on the client). The last line tells the remoteObject instance to pass any faults to the onFault method (also implemented in ActionScript). The example below shows the two event listeners that were registered above:
public function onFault(event:FaultEvent):void
Now the remote object can invoke the method on the server written in Ruby by simply calling:
and the results will be available in the event.result object that is passed to the onResult method as demonstrated above. In this example each property that is stored on the server is available as a property of the event.result object that we store above in the computerInfo instance. The source code for this example is available as part of the WebORB distribution for those interested in looking at the complete picture; after installing WebORB the server code is available in /app/services/InfoService.rb and the client code is available in /public/examples/example.mxml.
Advanced Features of WebORB
Consider the following Ruby class that can be dropped into a ProductService.rb file within /app/services:
This service will return all the products in the database and include associated image records; this type of service would be great for displaying a product list within a DataGrid that also could display product images upon selecting a row in the grid. On the client side the results of this method invocation could be made available in an ActionScript method such as:
public function onGetProducts(event:ResultEvent):void
Also note that within each product, another array is available that is accessed through the images property (the Image model in rails would likely include a URL or path attribute that Flex could use to load pictures). As an example, the following ActionScript method could be attached to the "change" event of the DataGrid:
public function onProductSelected():void
Last, remember that if you do not want to include associated data (has_many/belongs_to relationships within Rails models), then simply do not pass the :include option to your query.
Access to the HTTP Request and Session
session = RequestContext.get_session
Basic Authentication Support
This information is available using the same RequestContext API discussed above. Within your service you can get the credentials using the API demonstrated below:
The one problem with this approach is that now the service code is contaminated with authentication code. Ideally this would happen before ever reaching the method (like using a "before filter" in Rails controllers. WebORB will eventually provide an interface that will authenticate users in a similar way before ever reaching secure methods. In the meantime, it is important to make this feature available for developers who need this capability today (WebORB on Rails will continue to support this API even after a cleaner approach is provided).
Reader Feedback: Page 1 of 1
Latest AJAXWorld RIA Stories
|| | | | | | ||
| | | |
| | | |
| | | |
| | | |
|SYS-CON MEDIA: | | | ||
|SYS-CON EVENTS: | | | | ||
|INTERNATIONAL SITES: | | | | | | | | | ||
|Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.|
|Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.|