Ruby on Rails
Published in

Ruby on Rails

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

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

In Rails 4 app

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:

config/secrets.yml

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:

Rails 3 app

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.

--

--

--

Tutorials and opinions about development in Ruby on Rails.

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
Egor Vorobiev

Egor Vorobiev

business oriented web developer.

More from Medium

“Hello, World” in Ruby

3 steps for solving “Rails 7 app using devise cannot render error message”

Let’s Start Rebuilding rails

Build a new Ruby on Rails 7.0 Application