Skip to content

Instantly share code, notes, and snippets.

@toyflish
Created April 19, 2010 17:25
Show Gist options
  • Save toyflish/371318 to your computer and use it in GitHub Desktop.
Save toyflish/371318 to your computer and use it in GitHub Desktop.
rails 2.3.5, Activerecord-sessionstore takes session-id from get-vars if passed by shockwave flash (uploadify) using rack-middleware
# part of config/enviroment.rb
...
Rails::Initializer.run do |config|
# Load model files from sub folders
config.load_paths += Dir["#{RAILS_ROOT}/app/models/*"].find_all { |f| File.stat(f).directory? }
# Load middleware - initially used by flash-session-hack
config.load_paths << "#{RAILS_ROOT}/app/middleware"
....
# this file should sit at config/initializers/flash_session_middleware.rb
ActionController::Dispatcher.middleware.insert_before(
ActiveRecord::SessionStore,
FlashSessionCookieMiddleware,
ActionController::Base.session_options[:session_key]
)
@toyflish
Copy link
Author

identify session from Get-vars in Rails 2.3.5 using middleware and Activerecord-Sessionstore

after looking at some good writeups( thewebfellas and RailsTips ) that doesn't fit my conditions (Rails 2.3.5, Activerecord-Sessionstore) I got deeper into rack and made some small fixes.

4 ease steps to get it run

  1. put lash_session_cookie_middleware.rb into app/middleware directory
  2. add app/middleware to your config.load_paths like I did in enviroment.rb
  3. insert the FlashSessionCookieMiddleware into your middleware-stack by putting flash_session_middleware.rb into config/initializers directory
  4. pass your session-id to your swf - and make sure it passes it to your rails-app as a get-var ( in my view it can access it by calling :
    <%= request.session_options[:key] %>=<%= u cookies[request.session_options[:key]] %>
    don't forget to pass the authenticity_token as well:
    authenticity_token=<%= form_authenticity_token if protect_against_forgery? %>

if it doesn't work some hints to check

  • put 'debugger' call into FlashSessionCookieMiddleware::call to see if it is called
  • check your middleware stack in the console :

rake middleware

should show something like this

use Rack::Lock
use ActionController::Failsafe
use FlashSessionCookieMiddleware, "_session_id"  #<-- this is the right place for it!
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActiveRecord::SessionStore, #<Proc:0xb71ce794@(eval):8>
use ActionController::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActionController::StringCoercion
run ActionController::Dispatcher.new

make sure FlashSessionCookieMiddleware is in the right position ( it has to be before ActiveRecord::SessionStore). If you don't use ActiveRecord::SessionStore your stack will look different. Therefore you should gues wich is your SessionStore and apply this to your flash_session_middleware.rb file, where you define where to insert your FlashSessionCookieMiddleware

@swistaczek
Copy link

How can I use that middleware for Rails 3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment