18.12.2012 Views

Rails Magazine - Issue 3

Rails Magazine - Issue 3

Rails Magazine - Issue 3

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

36<br />

Ruby on <strong>Rails</strong> & Flex: Building a new software generation by Arturo Fernandez<br />

36<br />

When you write an application in Adobe Flex, you’ll end<br />

up with an executable that runs on the Adobe Flash Player,<br />

either inside a web browser or on another device.<br />

The back-end is a web application built using any technology<br />

like J2EE (Java Enterprise Edition), PHP or <strong>Rails</strong>. We’ll<br />

use <strong>Rails</strong> because it’s our favorite framework and because it is<br />

a good complement for Flex.<br />

The next step is to learn how the front end communicates<br />

with the back end. Let's get to the to code!<br />

How Flex Communicates With <strong>Rails</strong><br />

There are two ways to establish communication between<br />

Flex and <strong>Rails</strong>. The first is AMF (Action Message Format), a<br />

protocol to exchange data through traditional RPC (Remote<br />

Procedure Call). The second is HTTPService, a mechanism<br />

that allows Flex to make a call to the backend using the<br />

HTTP protocol.<br />

AMF is good for mapping objects on the server with<br />

objects in the client. Using AMF we can map an ActionScript<br />

object with a Ruby object, like a model instance. It’s also possible<br />

to call a controller method directly from ActionScript,<br />

sending an object as a parameter.<br />

Let see how to use AMF and HTTPService with Flex in<br />

practice.<br />

As an example, we’ll create an application with CRUD<br />

(Create, Read, Update, Delete) operations over a database<br />

table of products. First, we’ll create a front end in Flex, then a<br />

back end in <strong>Rails</strong>.<br />

First, we need to create a new Flex's project. The easiest<br />

way to do that is to use Flex Builder. Select File > New > Flex<br />

Project. Name the project and the main MXML file rails_<br />

magazine. Be sure to check “Web Application (Runs on Flash<br />

Player)”, as shown in Figure 1. This wizard will create all of<br />

the files needed by the application.<br />

The most important directory for us is src/. This is where<br />

our code files will reside. Using Flex Builder, we can build<br />

our GUI quickly via drag & drop. Widgets are on the left<br />

pane and workspace is the main area. A Datagrid is a great<br />

widget offered by Flex. It’s used to display data loaded from<br />

the database and allow typical operations like sorting and<br />

filtering.<br />

Figure 1. Creating a new Flex project<br />

Figure 2. Selecting public directory<br />

Figure 3. Building a GUI for our application<br />

The Flex binaries have to be accessible over the web. So<br />

we’ll put them in the public directory of the <strong>Rails</strong> application.<br />

This is easy, we only need to select this directory for Output

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!