Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rack::Session::Pool with Sinatra

I have a Sinatra webapp I've built using enable :sessions where I access my session data with session[:mything].

I now want to store data on the server side (ie. with database based sessions) and I can't figure out how to use Rack::Session::Pool, which appears to be the thing I need to use.

How do I go about converting my webapp for use with Pool?

I know I need to add the line

use Rack::Session::Pool

what comes next? — thanks in advance!

EDIT: Here's an example using cookie-based sessions:

require 'rubygems'
require 'sinatra'

enable :sessions

get '/' do
  session.merge!(params)
  session.inspect
end

Visit /?hi=there then visit / and you'll still see {'hi'=>'there'}, as it's been stored in a cookie.

like image 703
JP. Avatar asked Jul 26 '10 23:07

JP.


1 Answers

Simply replace the line enable :sessions with use Rack::Session::Pool. All enable :session does is adding Rack::Session::Cookie to the stack (which you want to avoid). The session helper will still work.

like image 64
Konstantin Haase Avatar answered Nov 14 '22 10:11

Konstantin Haase