开发者

Uploadify, Flash Sessions, and Rails 2.3.8

开发者 https://www.devze.com 2023-02-17 18:09 出处:网络
I\'m using Uploadify 2.1.4, and rails 2.3.8. I\'m trying to apply the FlashSessionCookieMiddleware fix that is supposed to maintain session information during the uploadify\'s flash-based request. I\'

I'm using Uploadify 2.1.4, and rails 2.3.8. I'm trying to apply the FlashSessionCookieMiddleware fix that is supposed to maintain session information during the uploadify's flash-based request. I've applied this fix after reading some of these guides:

  • Flash uploaders, Rails, cookie based sessions and CSRF: Rack Middleware to the rescue!

  • Uploadify on Rails with Paperclip

However, my session information is still not being carried over. Here's a look at my app configuration:

# config/environment.rb 

config.load_paths += %W( #{RAILS_ROOT}/config/middleware )

...

# config/middleware/flash_session_cookie_middleware.rb

require 'rack/utils'

class FlashSessionCookieMiddle开发者_如何学Goware
  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'])
      env['HTTP_COOKIE'] = [ @session_key, params[@session_key] ].join('=').freeze unless params[@session_key].nil?
    end
    @app.call(env)
  end
end

ActionController::Dispatcher.middleware.insert_before(ActionController::Base.session_store, FlashSessionCookieMiddleware, ActionController::Base.session_options[:key]) 

...

// My Uploadify JS 
'fileDataName'   : 'file',
'queueID'        : 'fileQueue',
'auto'           : true,
'multi'          : true,
'method'         : 'get',
'scriptData'     : { session_key: '<%= request.session_options[:key] %>'}

My middleware code is loading properly, Here's what rake middleware displays:

use Rack::Lock
use ActionController::Failsafe
use FlashSessionCookieMiddleware, "_my_app_session"
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActiveRecord::SessionStore, #<Proc:0xb7176760@(eval):8>
use ActionController::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActionController::StringCoercion
run ActionController::Dispatcher.new

Then, I try to test out if the last request has valid session/cookie info:

# In Controller
logger.info "Session: #{session.inspect}"

Which prints only an empty hash in log/development.log:

Session: {}

[Sniff] Can anyone see what I'm doing wrong?


I was able to get this to work on 2.3.8 with steps as posted here : http://railstips.org/blog/archives/2009/07/21/uploadify-and-rails23/

Only thing I had to modify was flash_session_cookie_middleware.rb to the following:

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/
        @session_key = ActionController::Base.session_options[:key]
        req = Rack::Request.new(env)
        unless req.params[@session_key].nil?
          env['HTTP_COOKIE'] = "#{@session_key}=#{req.params[@session_key]}".freeze
        end
      end

      @app.call(env)
    end
end


for those guys who want to solved the session problem by uploadify in Rails 3.0, please come here: http://www.damiangalarza.com/posts/ruby-on-rails/using-uploadify-with-rails-3/

0

精彩评论

暂无评论...
验证码 换一张
取 消