Skip to navigation

Disabling Rails sessions for JS and XML output

Published October 05, 2006

Every time your Rails app assigns a session to a visitor it creates a cookie with the session id and a relevant entry in the session store.

Whilst developing Web Connections I noticed sessions were being created for the JS and XML calls.

Apart from the performance overhead, you don’t really want to force cookies on users of the JS and XML output of your application.

So how do you disable sessions for the JS and XML requests? I just disabled sessions for all non-HTML requests:


class ApplicationController < ActionController::Base
  session :off, :if => lambda { |request|
    request.parameters[:format] &&
    request.parameters[:format] != 'html'
  }
end

Thoughts

toolmantim

I’m Tim Lucas, a user experience developer currently in Sydney Australia.

I occasionally write, snap photos, present on various technical topics, tweet my going-ons, share teh codes and post tidbits to the scrapbook.

Most recently I published Simplifying ticket sales on sydneyoperahouse.com (February 16, 2010)

Work with me via Agency Rainford, or shoot an email to and say hello.

Powered by the motion of the ocean