Rails Sessions Not Working with Safari? A Solution ...

Having trouble setting session variables in Safari and IE, but not Firefox?  I thought I'd share this quick tip to help save you some time and frustration. 

Bottom line: Don't use underscores in your hostname. Read on for more information.

The Problem

I was working on a rails application locally using passenger -- my development url was http://ux_prototype.local. I had everything working in my development environment so I pushed it out to production for April to check things out. Everything looked great and we were ready to roll.  Or so I thought... 

The app was throwing 500 errors in strange places whenever we were testing in Internet Explorer. 

Using Firefox, I was cruising along with no problems. Checking in Safari  from time to time things looked fine.   In Internet Explorer, things looked ok at first, however, when making a request for an action that altered session data (logging in or out), the 500 error appeared.  Going back to Safari I noticed that, in fact, it was having the same problems as IE.

Neither server-side clearing of session data (ActionController::Base#reset_session) nor client-side deleting the cookie from browser preferences helped at all. Using ./script/server to start Mongrel, however, did help. Could it be the hostname?

The Solution

Having a clearly defined problem revealed better results. As soon as I changed the hostname to http://ux-prototype.local, the problem went away.

So, don't use underscores in your hostname. While underscores are now considered "valid," many DNS clients and browsers will react with varying results. While this may be obvious to some, it created some frustration on my part and could easily be sidestepped in the future by avoiding this situation altogether.

Heres hoping it might save you some time and frustration as well!

Matt Swasey

Posted in Article Category: #Code