Sinatra sessions are not saved as expected

I am trying to use redirects and sessions in Sinatra to transfer some data around the site. Here's a simplified example of using PrettyPrint for debugging:

require 'pp'

require 'rubygems'
require 'sinatra'

enable :sessions

get '/' do
  session[:foo] = '12345'

  puts 'session1'
  pp session

  redirect to('/redir')
end

get '/redir' do
  puts 'session2'
  pp session
  'hello world'
end

Looking at the subtle conclusion, I see:

>> Listening on 0.0.0.0:4567, CTRL+C to stop
session1
{"session_id"=>
  "ea587d8afdcb2ada64f9b17cdd1fbae7b192dee5dfc2999ff9d323f1528f6a0f",
 "foo"=>"12345"}
127.0.0.1 - - [19/Jul/2011 10:33:24] "GET / HTTP/1.1" 302 - 0.0042
session2
{}
127.0.0.1 - - [19/Jul/2011 10:33:24] "GET /redir HTTP/1.1" 200 11 0.0004

Everything I saw in the docs suggests that this should work fine. In fact, I never get any session data for /redir, even if I request it directly, and the session is saved, as you would expect in subsequent requests for /.

Thoughts?

+5
source share
3 answers

, , . , , , :

get '/redir' do
  puts 'session2'
  puts session[:foo]
  pp session
  'hello world'
end

, Sinatra Rack. , [] ( ):

https://github.com/rack/rack/blob/master/lib/rack/session/abstract/id.rb

+1

FWIW, , , Sinatra , .

, , , "enable: sessions" "set: sessions, true" :

use Rack::Session::Cookie, :key => 'rack.session',
                           :path => '/',
                           :secret => 'your_secret'
+10

, Tom Lianza, , Sinatra

set :session_secret, SecureRandom.hex(64) To choose a random secret, and since there are several environments, each of them will use a different secret, resulting in conflicting cookies. Of course, it should be added as an environment variable or a Config file that is not verified by SVC.

Sinatra related issue

0
source

All Articles