Multiple local Rails apps and their sessions

October 25, 2006 Link to post  Permalink

If you are working on multiple Rails apps at a time, and run them all on localhost with different ports, watch out for session issues as the default _session_id cookie is shared between the apps. This is because the browser only uses the host name, not the host and port combination for looking up cookies.

What this means is, if you set some session state in one app, and then set some session state in a second app, the first app’s session will be orphaned and your first app’s session will be gone when you try to retrieve it.

There are two ways to have apps not interfere with each other:

Set each app to use a different cookie name.

In your environment.rb file:

1ctionController::Base.session_options[:session_key] = 'appname_session_id'

1ctionController::Base.session_options[:session_key] = appname_session_id

Use host mappings in /etc/hosts

In your /etc/hosts file, add a mapping for a host to your localhost IP address and use that in the browser. e.g,


In the browser, your reference this app as You still need to use a different port, but now the browser thinks it’s a different server and the cookies won’t be shared between the apps.

Interestingly, Windows has an /etc/hosts file in \windows\system32\drivers\etc, so this trick works there too!