3 steps to share a session between Rails 3 and Rails 4 applications

Egor Vorobiev
Jun 16, 2015 · 3 min read

For example we have two rails applications. An old one, based on Rails 3, and a new one based on Rails 4. And we want to have access to our old Rails 3 session in the new Rails 4 application.

For this reason we need to go through these simple 3 steps.

1. Set the session in two apps for the same domain.

The first requirement that I want to mark out: your domain should be equal for the two applications. Subdomains may be different, but the domain must be the same.

Because of that, go into config/initializers/session_store.rb and write something like this:

config/initializers/session_store.rb

These configs should be the same for two apps.

Notice:

By default, a cookie, which goes through https conntection won’t be avaliable in http connection. See this for more details. It means that cookies won’t be avaliable through http connection, if they are used in https. So, I propose you to enable https in the 2nd app.

2. Change cookie serializer for the Rails 4 application

If we don’t change the serializer, Rails 4.1+ won’t have the ability to deserialize information in your cookie. You can read more about this at The improved cookies serializer.

3. Disable encryption for Rails 4.

After creation, your Rails 4 application will have a similar config/secrets.yml file:

And your old Rails 3 application doesn’t have a secrets.yml file. Instead of that, it has config/secret_token.rb file like this:

secret_key_base needs to encrypt the cookies for your rails application. Rails 3 doesn’t have this ability, so we need to just disable it in the Rails 4 app. You need to substitute the secret_key_base keyword with secret_token in rails 4 app. So, your new config/secrets.yml should looks like this:

This substitution disables encryption and enables signing for your session.

The token in two apps should be the same

I hope this topic will solve your problem. If something will go wrong or you have a question, you can leave a comment below. Thank you so much for reading.

Ruby on Rails

Tutorials and opinions about development in Ruby on Rails.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store