Flex on Rails book source code updated for Rails 2.3.2

Sunday, May 3, 2009

Rails 2.3.2 introduces some changes that required that we adapted the code from our book. I just reviewed and update the code for chapter 2,3,4,7,18,20 and 22 to run with Rails 2.3.2 and Flex SDK 3.3. Tony will do some more work on checking RubyAMF and the chapters he wrote code for. So here is quick look at the changes:

- In Rails 2.3.2 the file containing the application controller class must be renamed from application.rb to application_controller.rb.

- Another change introduce since the new rack middleware stack is that adding _method=put to the url isn't recognized by rails in the case of a post. However it's still recognized for the delete, so I am not sure what's going on and if the Rails guys will reinstate the _method= in the near future. Fortunately Rails recognizes the X_HTTP_METHOD_OVERRIDE request header. So for updates we set this header in Flex. Now Flex doesn't set the headers if the body of the message is empty like for delete, so we still use the _method=delete for deletes. It would be nice to be able to set this in a consistent manner for both deletes and updates.

- On chapter 04 on testing with Fluint we moved to using fluint 1.1.1, and no changes was require for this.

- On chapter 20 Server Push with Juggernaut I updated using the latest plugin downloaded from github and no more the code from rubyforge and updated gem version for eventmachine to 0.12.6 and the json gem to 1.1.4. So installing the plugin is now done with the following command:


./script/plugin install git://github.com/maccman/juggernaut_plugin.git


If you find any other issues with Rails 2.3.2. send me an email or leave a comment on this blog post. Thanks to all the readers that submitted comments so far.

Enjoy!
Daniel

Labels: ,

Flex on Rails presentation at RMAUG on March 10th

Sunday, March 15, 2009

This is the edited version of the talk Tony Hillerson and Daniel Wanja gave at the Rocky Mountain Adobe User Group on March 10th. There is an echo the first two minutes, then the sounds stabilize. We also had a software issue on Tony's notebook and display port issue on Daniel's, parts which I edited out. The demo gods where not with us that night :-), but we had fun and I hope we answered many of the questions the attendance had. Also we wanted to tailor the talk on Ruby on Rails rather than on Flex and be an open format, but we had more questions than anticipated and presented only 10% of the material we had.


Flex on Rails presentation at RMAUG on March 10th from daniel wanja on Vimeo.
Note: during the first 2 minutes there is an echo. Sound skips from time to time throughout the video.

The talk was recorded by RMAUG and posted on their blog, and can be viewed using Connect here.

Thanks again to everyone who attended!

Daniel.

Labels: , , ,

File Upload with Multipart Request - revisited

Monday, March 9, 2009

In the file upload chapter of the book I show how to create a multipart request to upload an image to a Rails server. On a client project I needed to send some XML and two images in one post to a Rails application. Mike, over at http://www.mikestead.co.uk, wrote a nice framework to create multipart requests in an ActionScript like fashion which would make this easier. You can read the whole article over at http://www.mikestead.co.uk/2009/01/04/upload-multiple-files-with-a-single-request-in-flash/. However, I tried his out and it didn't work with Rails. After some debugging and a little tweaking I made it work, you will find the fix at the end of this article. So let's look at an example (from my client project, used without authorization ;-):

private function sendToServer(planId:String, plan:XML, patternBitmap:Bitmap, layoutBitmap:Bitmap):void {
var variables:URLVariables = new URLVariables();
variables.quilt_plan = plan.toXMLString();
variables.pattern_image = new URLFileVariable(new PNGEncoder().encode(patternBitmap.bitmapData), "pattern_image.png");
variables.layout_image = new URLFileVariable(new PNGEncoder().encode(layoutBitmap.bitmapData), "layout_image.png");
var request:URLRequest = new URLRequestBuilder(variables).build();
request.url = "/quilt_plans/";
request.method = URLRequestMethod.POST;

loader = new URLLoader(request);
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);
}



As you see using the URLFileVariable and URLRequestBuilder is pretty straight forward. In the above example we first add some XML to the URLVariable:

variables.quilt_plan = plan.toXMLString();


Then we add the bitmap data of an image:

variables.pattern_image = new URLFileVariable(new PNGEncoder().encode(patternBitmap.bitmapData), "pattern_image.png");


We add a second image named layout_image in the similar manner. Finally we use the build method of the URLRequestBuilder to create a multipart request.

var request:URLRequest = new URLRequestBuilder(variables).build();


This request can then be passed to the load method of a standard URLLoader to send two images and an XML document in one swoop.

On the Rails side in our controller

    @quilt_plan = QuiltPlan.new(:quilt_plan_calculations => params[:quilt_plan])
@quilt_plan.build_layout_image(:uploaded_data => params[:layout_image])
@quilt_plan.build_pattern_image(:uploaded_data => params[:pattern_image])



Now the params argument passed to your Rails action method contains the XML in the params[:quilt_plan] variable, and the two images ready for upload in params[:layout_image] and params[:pattern_image]. You could do params[:layout_image].read to get the image, or as with our example the attachment_fu plugin does it for us.

Et voila...happy multipart requesting!

Rails did have issues with the way the request was created and I applied the following changes which mainly just changes the Line Feed send in the requests in URLRequestBuilder.as:


< private static const LF:String = "\r\n";
---
> private static const LF:String = "\n";
187,188c185
< field.writeUTFBytes(MULTIPART_MARK + MULTIPART_BOUNDARY + LF +
---
> field.writeUTFBytes(LF + MULTIPART_MARK + MULTIPART_BOUNDARY + LF +
191a189
>
208,209c206
< field.writeUTFBytes(MULTIPART_MARK + MULTIPART_BOUNDARY + LF +
---
> field.writeUTFBytes(LF + MULTIPART_MARK + MULTIPART_BOUNDARY + LF +


Enjoy!
Daniel

Upside down chapters?

Thursday, January 29, 2009

The other day while giving away seven books at the Derailed (Denver Ruby on Rails User Group) one attendee noticed that one the books had some chapters that where upside down (around page 166). I didn't think much of it, but today a coworker from Tony who bought the book noticed the same thing. All the pages are there but for some of the chapters you will need to turn the book upside down. Please drop us a line (d[at]n-so.com) if you experience the same. I am not sure how broad the issue is and in fact it's out of my hands, but we would like to keep the publisher informed of this. I am not sure how they deal with a situation like that, but let's find out.


Labels:

Moving RailsConf apps to herokugarden.com

Monday, January 26, 2009

Three applications from our RailsConf talk where deployed using heroku.com. I just realized last week that they moved the apps that where deployed on their closed beta environment to their new free offering herokugarden.com environment. heroku.com will be the place for their commercial Rails hosting offering. No need to say that I am waiting eagerly to try it out. The impact was that the application they moved to herokugarden required a migration to be run. But besides that the move was straight forward. A couple of things to know which are described more in details on their transition page but to migrate an existing app to the garden if followed these three steps:

1. 'sudo gem install herokugarden'
2. From your application folder 'herokugarden git:transition'
3. If you encounter an Application Failure doing a 'git push', you need to fix your .git/config file as follows:

[remote "heroku"]
url = git@heroku.com:rc-10-twitterfriends.git
fetch = +refs/heads/*:refs/remotes/heroku/*

to

[remote "origin"]
url = git@herokugarden.com:rc-10-twitterfriends.git
fetch = +refs/heads/*:refs/remotes/origin/*


You can download the new apps here:

http://www.flexonrails.com/air_apps/apps/S3Browser.air
http://www.flexonrails.com/air_apps/apps/PhotoBooth.air
http://www.flexonrails.com/air_apps/apps/TwitterFriends.air

Flex on Rails Talk in Denver: Thank you!

Friday, January 23, 2009

Thanks to everyone that attended, it was a fun meet.
You can fine the slides here after and the link to the source on http://www.flexonrails.com/flex_apps.html.

Flex On Rails talk in Denver - Thursday 22nd

Monday, January 19, 2009

Tony and I will be giving a talk on Flex on Rails at Derailed, the Denver Rails User Group. This will take place at Forest Room 5 in LODO at 6:30pm, this coming Thursday 22nd.

On the agenda.

  • 6:30 – 7:00: Meet and mingle

  • 7:00 – 8:30: Flex on Rails – From the authors Daniel Wanja and Tony Hillerson

  • 8:30 – ... : Book raffle and spirits.


We will show highlights of each of the chapters and samples from the book.

I hope to see you there!
Daniel
© 2008 Flex On Rails. All Rights Reserved