Uploadify,Flash Sessions和Rails 2.3.8

时间:2011-03-20 01:49:50

标签: jquery ruby-on-rails session

我正在使用Uploadify 2.1.4和rails 2.3.8。我正在尝试应用FlashSessionCookieMiddleware修复程序,该修复程序应该在uploadify的基于闪存的请求期间维护会话信息。在阅读了其中一些指南后,我已应用此修复程序:

但是,我的会话信息仍未被转移。以下是我的应用配置:

# config/environment.rb 

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

...

# config/middleware/flash_session_cookie_middleware.rb

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'])
      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] %>'}

我的中间件代码正常加载,以下是 rake中间件显示的内容:

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

然后,我尝试测试上一个请求是否有有效的会话/ cookie信息:

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

在log / development.log中只打印一个空哈希:

Session: {}

[嗅]任何人都可以看到我做错了吗?

2 个答案:

答案 0 :(得分:1)

我能够通过此处发布的步骤在{2.3}中使用http://railstips.org/blog/archives/2009/07/21/uploadify-and-rails23/

我唯一需要修改的是flash_session_cookie_middleware.rb到以下内容:

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

答案 1 :(得分:0)

对于想要通过Rails 3.0中的uploadify解决会话问题的人,请来这里:http://www.damiangalarza.com/posts/ruby-on-rails/using-uploadify-with-rails-3/