Rack-based Session Stores on Rails

Posted by Christopher Rigor on December 22, 2008 
Filed Under Ruby on Rails, Web Development

Rails edge is now using Rack-based session stores. That means you can access the same session on any Rack applications. Read about it on the Rails blog.

Nothing changes though on how you use sessions on Rails. On your Rails app, you’ll have something like


session[:user] = some_user_id

Let’s see how we can access the same session on a Rack application. You can read about Rack here.

Rack handles session using Rack::Session::Cookie. Create a file config.ru


use Rack::Session::Cookie, :key => '_railssession_session', :domain => 'railssession.labs.admoolabs.com'
run Proc.new {|env| [200, {"Content-Type" => "text/html"}, "Rack session 'user' has a value of #{env['rack.session'][:user] || 'nil'}."]}

The key should be the same key that you used on config/environment.rb on your Rails app.

Check out the demo. Set session[:user] on the Rails app, then view the session on the Rack app.

Comments

2 Responses to “Rack-based Session Stores on Rails”

  1. Bala Paranj on December 22nd, 2008 7:17 AM

    Awesome easy to understand example. How can I share this session info between two Rails apps? I want to implement single sign on.

  2. Diigo’ the week (weekly) » The Bipeds’ Monitor on January 11th, 2009 8:35 AM

    [...] Rack-based Session Stores on Rails : Admoo Labs [...]

Leave a Reply