Sinatra clears session on post
I was suffering from the same issue as you: sessions were being cleared on post.
I have no idea why this works, but this is my solution:
#enable :sessions
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'your_secret'
I literally just replaced the enable :sessions
bit with use Rack::Session::Cookie ...
and now all is good in the world.
Deleting the current session with Rack::Session::Cookie
Just use
session.clear
to destroy the session.
Sinatra and session variables which are not being set
If you're using Shotgun, add the following line to the configure block:
set :session_secret, "My session secret"
To quote from rkh, Sinatra's current maintainer:
[Shotgun] will restart the server on every request, thereby regenerate the session secret and thus invalidate your sessions. This has been fixed in current master. Simple fix: set the session_secret option.
NOTE: This fix doesn't work if you use Rack::Session::Pool
Getting unique session ID in Sinatra
From what I can tell JSESSIONID
is used to pass the session around in a query string, and Sinatra doesn't have something like that, at least not easily accessible. Sinatra uses Rack for session management, and by default uses a cookie to store all session data. There are other session options in Rack, like memcached, where a unique session id is stored in a cookie, but even there Rack abstracts that away so you don't ever need to see the session id (though it is still accessible, see the documentation).
If you want to go that route then look into messing with the Rack middleware in Sinatra, but if all you need is a unique id, then it would probably be easier to generate one yourself and store it in the session.
Related Topics
Travis Reports Odd Message of Corrupted Gemfile.Lock
How to Improve Jruby Load Time
Is There an Equivalent Null Prevention on Chained Attributes of Groovy in Ruby
What's the Best/Easiest Gui Library for Ruby
How to Get Ruby to Print a Full Backtrace Instead of a Truncated One
Format the Date Using Ruby on Rails
Undefined Method with "_Path" While Using Rails Form_For
Unix Commands Work on Server But Not in Ruby Ssh Session
Undefined Local Variable for Hash in Method Ruby
How to Dump Strings in Yaml Using Literal Scalar Style
Nokogiri - Error: Failed to Build Gem Native Extension
How to Run a Single Test/Spec File in Rspec
Array Include Any Value from Another Array
Add Image to Layout in Ruby on Rails