Developers can often be seen working on Ruby on Rails projects that implement the ability of uploading photos onto the servers. Some of these projects may need the ability of uploading multiple files in one go and the developer may need a workable solution as multi file uploads may not be possible in case of HTML 5. As an alternative, they can make use of a Flash-based upload, Uploadify to be precise, which is actually a jQuery plug-in that gives the ability of uploading multiple files via Flash-based interface. This interface gives helpful functionalities like multi upload queuing and progress bars apart from being very easy to use.
There are plenty of resources to make this work with a Ruby on Rails application, however one of the main issues that the developers may face is that the plug-in can be used mostly for Ruby on Rails 2.3.8 only. So, if one is using Ruby on Rails 3, the widely available methods online may not be of much use.
So, why do you need to put in all this extra work?
Before delving into the solution on how to get it working, let’s first take a look at why it’s not working in the first place. It’ll help us understand the problem we’re dealing with and how to go about fixing it.
As you may know that the request made through DELETE, PUT, POST all need a valid and working authentication token apart from session information for successful completion of the request. Doing so also protects against the cross-site request forgery. A hidden input gets created (consisting of an authenticity token for proving the request’s authenticity) along with the session cookie data when you’re building forms with Rails form helpers.
This leads to a couple of problems while using Flash for uploading the files. First of all, Uploadify will not automatically send an authenticity token along with the POST request. Secondly, the Flash-based requests don’t normally send the session related information with the requests. Although the first problem is not too hard to fix, sending of session related data may need some work.
Sending of the authentication token
Once we get these meta tags in their place, we can add authenticity to the parameters that are normally sent with the Uploadify’s request.
Passing of the session cookie
To correctly associate the session information with the request, the programmer will need creation of some custom middleware that’ll intercept requests coming in from Flash and inject cookie data into the header.
With all of the above in place, you’ll be ready to successfully upload multiple files using Flash along with your Ruby on Rails project.