Uploadify on Rails with Paperclip
For a project I'm working on, I was hoping to implement a flash upload solution that would allow the site admins to upload multiple files at once. In the past I have used swfupload for such projects at work and beyond, but I was hoping to find something that used jQuery.
Meet Uploadify
Uploadify is a jQuery plugin that had all the features I was looking for, all packaged up with a super slick looking website too boot. Since I already had jQuery in my rails project, I figured this would be a snap.
Integrating Rails Sessions With Flash
The biggest challenge of using a flash-based uploader in rails is the lack of the cookie/session data being passed along with the flash-based request. This of course poses a few hurdles when attempting to secure your file uploads. Fortunately I found a great blog post to following along with.
For those that don't want to dredge through comments and linked github projects, I will summarize the key bits here. The first step is to create a new middleware file in RAILS_ROOT/middleware:
Many of the examples on the web right now didn't work for me as the params hash wasn't marshaling elements in as symbols for the keys, but rather strings. I'm not certain if this is specific to Rails 2.3.2, my flash client, or the uploadify swf file itself. Then you need to inform rails to use this new middleware piece by editing your session_store.rb initializer:
Uploadify Time
Now with the middleware magic in place, you can now start using some uploadify js in your views:
I created a simple helper method to return the session key based off the blog post linked above. I won't bore you with that minor detail at this time. Simply modify the uploadify config to match the locations of where you have placed the swf file along with the image file in your environment and you'll be all set.
I'm using Paperclip to save the attachments on the back-end to complete this dead-simple uploading tool, but any other plugin would work just as well.