Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created June 9, 2011 21:15
Show Gist options
  • Save JoshCheek/1017771 to your computer and use it in GitHub Desktop.
Save JoshCheek/1017771 to your computer and use it in GitHub Desktop.
Sinatra + Heroku Session Bug

When using Sinatra on Heroku, you can set your app up in such a way as to have a bug where your sessions will work locally but not on Heroku. The bug arises when you have the lines enable :sessions and use Rack::Session::Cookie both in your app. The solution I found was to remove the line enable :sessions.

require "bundler/setup"
require 'sinatra'
get '/a' do
to_display = session[:to_remember]
session[:to_remember] = "a"
to_display
end
get '/b' do
to_display = session[:to_remember]
session[:to_remember] = "b"
to_display
end
enable :sessions # <-- this line will cause it to work locally but not on Heroku, just delete it.
use Rack::Session::Cookie
run Sinatra::Application
source "http://rubygems.org"
gem "sinatra"
GEM
remote: http://rubygems.org/
specs:
rack (1.3.0)
sinatra (1.2.6)
rack (~> 1.1)
tilt (< 2.0, >= 1.2.2)
tilt (1.3.2)
PLATFORMS
ruby
DEPENDENCIES
sinatra
@celeryclub
Copy link

This was a life saver for me. Seems like use Rack::Session::Cookie and enable :sessions don't play well together in general.

@Taormina
Copy link

enable :sessions turns on a bunch more options, but by default also does Rack::Session::Cookie. It's mostly authentication options (which you should look into).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment