enable :sessions
get '/foo' do
session['m'] = 'Hello World!'
redirect '/bar'
end
get '/bar' do
session['m'] # => 'Hello World!'
end
它似乎不起作用。
答案 0 :(得分:14)
configure(:development) { set :session_secret, "something" }
Sinatra 1.3将不再需要这样做。
答案 1 :(得分:11)
也许您的网络浏览器禁用了Cookie? Sinatra的会话默认使用cookie。
这是我的测试应用程序:
require 'sinatra'
enable :sessions
get '/foo' do
session['m'] = 'Hello World!'
redirect '/bar'
end
get '/bar' do
<<-ENDRESPONSE
Ruby: #{RUBY_VERSION}
Rack: #{Rack::VERSION}
Sinatra: #{Sinatra::VERSION}
#{session['m'].inspect}
ENDRESPONSE
end
这就是行动:
phrogz$ curl --cookie-jar cookies.txt -L http://localhost:4567/foo
Ruby: 1.9.2
Rack: [1, 1]
Sinatra: 1.2.3
"Hello World!"
phrogz$ curl -L http://localhost:4567/foo
Ruby: 1.9.2
Rack: [1, 1]
Sinatra: 1.2.3
nil
phrogz$ cat cookies.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
localhost FALSE / FALSE 0 rack.session BAh7BkkiBm0GOgZFRkkiEUhl...
如果没有cookie,您的重定向将会起作用,但它会像重定向后的全新会话一样,会话从头开始。