John Nunemaker posted a great article on using uploadify with rails.

Uploadify is a sweet jquery plugin that lets you do multiple file upload with progress indicators and everything, using the magic of flash.

But you know that magic always comes with a price. Specifically: flash sucks. It sucks in part because it doesn't let you send cookies back with your request. John's post showed you a way around that.

But it also sucks because it doesn't send HTTP request headers either. Which means no RJS, kiddies. Unless you use a workaround like so.

In your middleware:


require 'rack/utils'

class FlashSessionCookieMiddleware
  def initialize(app, session_key = '_session_id')
    @app = app
    @session_key = session_key
  end

  def call(env)
    if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/
      params = ::Rack::Utils.parse_query(env['QUERY_STRING'])

      unless params[@session_key].nil?
        env['HTTP_COOKIE'] = "#{@session_key}=#{params[@session_key]}".freeze
      end

      # Here's the goods
      unless params['_http_accept'].nil?
        env['HTTP_ACCEPT'] = "#{params['_http_accept']}".freeze
      end
    end

    @app.call(env)
  end
end

And in your javascript, you'll need to add a bit of code to eval the response:

$("#uploadify").uploadify({

  // Eval the response
  'onComplete'      : function(a, b, c, response){ eval(response) },

  // And set the accept header to application/javascript
  'scriptData'      : {
    '<%= session_key_name %>' : '<$%= u cookies[session_key_name] %>',
    '_http_accept': "application/javascript"
  }, 

  ... Other options removed for clarity ...

});      

Now, bingo! You can just use responds_to like normal.