Last Updated: February 25, 2016
·
12.81K
· Peter Boling

CookieOverflow in Rails 4

Rails 4 encrypted cookie store is awesome:

MyApp::Application.config.session_store :encrypted_cookie_store ...

I switched! And then I was immediately hit with cold bucket of water right after a facebook / devise / oauth:

ActionDispatch::Cookies::CookieOverflow

So if you were just under 4kb before you will split your britches! Switch to active record session store. First comment out the :encryptedcookiestore line in config/initializers/session_store.rb.

Uncomment config/initializers/session_store.rb to:

MyApp::Application.config.session_store :active_record_store

Add this to your gemfile:

gem 'activerecord-session_store', github: 'rails/activerecord-session_store'

Then install:

bundle install

Then create the migration:

bundle exec rails g active_record:session_migration

Then run migrations:

bundle exec rake db:migrate
RAILS_ENV=test bundle exec rake db:migrate

Then restart the app server!

6 Responses
Add your response

It not works for me... i'm novice in rails, i did gem install activerecord-sessionstore, then bundle install, finally bundle exec rails g activerecord:session_migration, into my app, but this gave me an error : not could generator activerecord-session:migrate, what is the solution?

over 1 year ago ·

I've just run into a similar issue, so this is super handy thank you!

One question though, seeing as though you are changing the session_store initializer, that means this change affects all environments, how would you set this just for test ?

over 1 year ago ·

@fabian818, I did not say to do gem install. I said to add the gem to your Gemfile and then bundle install. If you skipped that then you will get the error you got.

over 1 year ago ·

@brad8711 - if you only want to make the change in test, then don't change the initializer, and add this line of code:

MyApp::Application.config.session_store :active_record_store

Somewhere that is only loaded by the test env. Perhaps config/environments/test.rb, or spec/spec_helper.rb.

Only problem is then you'll have a database schema that has different needs in different environments.

I would strongly discourage you from doing this.

If you use it, use it everywhere.

over 1 year ago ·

I'll give that a go,
I tried putting
MyApp::Application.config.sessionstore :activerecord_store
into the MyApp::Application.config do loop in test.rb but that didn't seem to do the job.

over 1 year ago ·

I don't understand how I got that error. I made some very unrelated changes to my code, nothing to do with sessions... :(. I ll give this a try anyway.

over 1 year ago ·