JP.
JP.

Reputation: 5594

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.

Upvotes: 10

Views: 3834

Answers (1)

Konstantin Haase
Konstantin Haase

Reputation: 25964

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.

Upvotes: 13

Related Questions