Aug 27 2006

Mongrel for Flex and Rails development

Tag: RailsDerek Wischusen @ 9:56 pm

Most, if not all, all of the tutorials that I have seen about integrating Flex with Rails have used the WEBrick server that is included with Ruby.  While there is nothing wrong with using WEBrick for developing small apps on your local machine, I would recommend that most developers use Mongrel, especially if you are developing in a windows environment.

Mongrel (http://mongrel.rubyforge.org/) is going to get you much better performance than the CGI based WEBrick and it is just as easy to use.

To get Mongrel, make sure you have the latest versions of Ruby, Rails, RubyGems installed, then just run gem install mongrel from the command line and then follow the instructions on the screen.

To run Mongrel just go to the rails application that you want to run on the command line and run mongrel_rails start.

That’s it.  Just as easy as WEBBrick, and now your running in an environment that could be deployed for small scale apps.

Also, in honor of Zed Shaw’s (the creator of Mongrel) site here is a picture of my mongrel (Eva)

 My mongrel


Aug 27 2006

Simple FTP app using Flex and Rails

Tag: Flex and RailsDerek Wischusen @ 9:05 pm

Here is a simple app that I put together to demonstrate an integration between Flex and Ruby’s File and Directory utilities. It is not, by any means, meant to be a full FTP application (though I suppose it could, with some work, serve as the foundation for one).

Here is what you can do with it:

  • Create a new account or log into an existing account.
  • Create new subfolders in your account.
  • Upload .txt or .jpg files. Up to 1MB per account (look out Box.net).
  • Download files.
  • Delete files or folders.
  • Move files or folders by dragging them from the right panel dropping them on directory tree.

That’s about it.

Please consider this to be an alpha realease, as I am sure there will be bugs. If you do encounter a bug that you would like to report, please send an email to: bugs at flexonrails dot net

Finally, here is the link to the app: http://www.flexonrails.net/flexftp/public/

I will release the source as soon as I’ve had a chance to fix all the bugs and clean up the code a bit.

Credits:

[UPDATE 04/22/2007]

Ok, I never did get around to cleaning up the code for this app so the code is still in pretty rough shape, but a couple of people asked for the source recently, so I’ve decided to release the source as is in the hopes that at least some small parts of it will be useful to someone.

Download Source Here


Aug 17 2006

Save your CSS with the Flex 2: Styles Creator

Tag: Flex and RailsDerek Wischusen @ 9:07 pm

A while back I was playing around with the Flex 2: Styles Explorer (Developed by Adobe Consulting) and I thought to myself that it would be nice if you could save the CSS that you create with it.  Well, a little AS3 and Ruby later (see the previous post for details) and I present to you The Flex 2: Styles Creator. 

You can check it out here: http://www.flexonrails.net/stylescreator/public/

Simply use it as you would the Flex 2: Styles Explorer, but if you create something you really like, just click the Download CSS button (my minor contribution) in upper-right hand corner and download it to your machine.

In the future, I plan to build out the back-end so that you will be able to save to a database and share your Styles with everyone else.  I hope to get this done in a couple of weeks.


Aug 17 2006

Simple Ruby script for uploading files from Flex

Tag: Flex and RailsDerek Wischusen @ 10:06 am

Here is the AS3:

private var fileRef:FileReference = new FileReference();
private function uploadFile():void
{
 fileRef.addEventListener(Event.SELECT, selectHandler);
 fileRef.addEventListener(Event.COMPLETE, completeHandler);
 

try {
      var success:Boolean = fileRef.browse();
 } catch (error:Error) {
      trace("Unable to browse for files.");
  }
}
private function selectHandler(event:Event):void {

    // assumes the Ruby Method is in the application controller
    var request:URLRequest = new URLRequest("application/upload_file")
    try {
        fileRef.upload(request);
    } catch (error:Error) {
        trace("Unable to upload file.");
    }
}
private function completeHandler(event:Event):void {
 fileRef.removeEventListener(Event.SELECT, selectHandler);
 fileRef.removeEventListener(Event.COMPLETE, completeHandler);
 trace("uploaded");
} 

  

Here is the Ruby:
    def upload_file
      file_data = params[:Filedata]
      file_name = params[:Filename].to_s
      File.open(file_name, "wb") { |f| f.write(file_data.read) })
      render(:xml => "< success />" )
    end

That’s it.  Note, that if your running on a non-windows box you should change “wb” to “w”.


Aug 17 2006

Downloading data from Flex using Ruby send_data

Tag: Flex and RailsDerek Wischusen @ 10:00 am

I recently encountered a situation where I wanted to be able to download data from Flex directly to the user’s desktop.  Fortunately, Ruby provides a very simple way to do this using the send_data method, which sends data to the browser for download.  However, the way that you need to communicate with this method for it work properly is slightly different from how you would normally communicate between Flex and Rails. 

Usually, when you want to call a Ruby method you would use an HTTPService, but when you are calling send_data this will not produce the desired result. This is because the data will be returned back to the HTTPService inside of Flex, which is pretty useless (you are basically passing the data back and forth).  What you want is for the data to be returned to the browser so that the user can download it.

To solve this problem I used the Flex navigateToURL method along with a URLVariables object:

 private function saveToFile():void
  {
  var appUrl:URLRequest = new URLRequest();
  appUrl.url = "application/data_to_file";
  appUrl.method = URLRequestMethod.POST;
  
  var variables:URLVariables = new URLVariables();
  variables.content = "Downloaded from Flex!";
  variables.suggested_name = "download.txt";
           
  appUrl.data = variables;
  

  navigateToURL(appUrl, "_top");

}

The URLRequest object specifies the url of the method to call (the data_to_file method inside the application controller), the URLVariables object specifies the data to pass, and the navigateToURL method sends HTTP request through the browswer to data_to_file, which sends the data back to your browswer, which prompts you to download the file.

Here is the Ruby method:

    def data_to_file
      file_data = params[:content]
      suggestion = params[:suggested_name]
      send_data(file_data, :filename => suggestion)
    end

To see a simple example of this, check out the following post.


Aug 06 2006

Day 1

Tag: UncategorizedDerek Wischusen @ 7:23 pm

This site, whenever I get around to setting it up, will be dedicated to the Flex RoR community (all five of us).