Rails Magazine - Issue 3
Rails Magazine - Issue 3
Rails Magazine - Issue 3
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
folder as shown in Figure 2. Once we’re finished with the<br />
GUI, we can create the back-end.<br />
First, we’ll create a <strong>Rails</strong> application using the rails command.<br />
Second we will need a model to store the product's<br />
data so we will execute this command:<br />
$ script/generate model Product name:string<br />
description:string price:float<br />
Now we need to configure our database. By default <strong>Rails</strong><br />
2 offers a simple configuration for an SQLite database, so we<br />
will use that. The next step is to run the migration:<br />
$ rake db:migrate<br />
Now we need a controller to manage the client's requests:<br />
$ script/generate controller products<br />
Our backend is finished for now. The next step is to configure<br />
connection between it and the front-end.<br />
RubyAMF<br />
In this example, we’ve created two datagrids (Figure 3).<br />
One will use AMF to communicate with the server. The other<br />
will use HTTPService. We’ll cover AMF First.<br />
Fortunately for <strong>Rails</strong> developers, we have the RubyAMF<br />
rails plugin which will do a lot of the heavy lifting. Installation<br />
is easy. Just type the following:<br />
$ script/plugin install http://rubyamf.googlecode.com/<br />
svn/tags/current/rubyamf<br />
When installation is finished we will be ready to write a<br />
little code for our <strong>Rails</strong> application. In the config folder we can<br />
find a file called rubyamf_config.rb that includes the mapping<br />
between our ActiveRecord class and our ActionScript class.<br />
But we don’t have an ActionScript class! Let’s write one. It<br />
will map to the product model that we just created in rails. In<br />
the Flex project, create a new subfolder of src called valueobject<br />
and new ActionScript class called Product. This is the code<br />
for the new Product.as file:<br />
package valueobj<br />
{<br />
[RemoteClass(alias=”Product”)]<br />
public class Product<br />
{<br />
public var id:int;<br />
public var name:String;<br />
public var description:String;<br />
public var price:Number;<br />
public var createdAt:Date;<br />
public var updatedAt:Date;<br />
Ruby on <strong>Rails</strong> & Flex: Building a new software generation by Arturo Fernandez<br />
}<br />
}<br />
/**<br />
* Constructor<br />
*/<br />
public function Product()<br />
{<br />
}<br />
Notice that the public attributes of this class match those<br />
in our <strong>Rails</strong> model. We also declare that RemoteClass is Product<br />
– our <strong>Rails</strong> model class.<br />
Now go back to rubyamf_config.rb and add these lines of<br />
code:<br />
ClassMappings.register(<br />
:actionscript => ‘Product’,<br />
:ruby => ‘Product’,<br />
:type => ‘active_record’,<br />
:attributes => [“id”,”name”,”description”, “price”,<br />
“created_at”, “updated_at”]<br />
)<br />
Wings to the moon<br />
Now that we’ve mapped the Flex Product class to the <strong>Rails</strong><br />
Product class, we can load the data into our grid. You’ll need to<br />
set the dataProvider property of the datagrid component:<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
37<br />
37