Cookie overflow in rails application?

Ruby on-Rails

Ruby on-Rails Problem Overview


> ActionDispatch::Cookies::CookieOverflow in UsersController#create

I have this error when I try to open the page. I do not know how to debug this error. Do you have any suggestion for this problem?

def create
  @user = User.new(params[:user])
  sign_in @user
  
  if @user.save
    @user.folders.create(:folder_name=>"Default Folder", :user_id=>@user.id)
    flash[:success] = "Welcome to Bunch<it>! "
    redirect_to @user
  else
    @title = "Sign up"
    render 'new'
  end
end


def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  session[:current_user] = user
  current_user = user
end

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You've got a 4kb limit on what you can store in a cookie, and when Rails converts your object into text for writing to the cookie its probably bigger than that limit.

Ruby on Rails ActionDispatch::Cookies::CookieOverflow error

That way this CookieOverflow Error occurs.

The easiest way to solve this one is, you need change your session_store and don't use the cookie_store. You can use the active_record_store by example.

Here is the steps

  1. Generate a migration that creates the session table

     rake db:sessions:create
    
  2. Run the migration

     rake db:migrate
    
  3. Modify config/initializers/session_store.rb from

     (App)::Application.config.session_store :cookie_store, :key => 'xxx'
    

    to

     (App)::Application.config.session_store :active_record_store
    

Once you’ve done the three steps, restart your application. Rails will now use the sessions table to store session data, and you won’t have the 4kb limit.

Solution 2 - Ruby on-Rails

To make the :active_record_store functionality works in Rails 4/5, you must add the activerecord-session_store gem to your Gemfile:

gem 'activerecord-session_store'

then run the migration generator:

rails generate active_record:session_migration
rake db:migrate

And finally set your session store in config/initializers/session_store.rb:

Rails.application.config.session_store :active_record_store, :key => '_my_app_session'

UPDATE:

If anyone is receiving a null value in column "session_id" violates not-null constraint message in rails 4, there's a workaround in github(not tested). You must to create an initializer with ActiveRecord::SessionStore::Session.attr_accessible :data, :session_id

Solution 3 - Ruby on-Rails

If you're seeing this, check that you're not blowing up some session data. In my case, it was thousands of the same message pumped into the flash message. Just saying.

I'll add that if you think the solution is to make your cookie store bigger (as most of the other answers address), you're probably better off rethinking what you're actually putting in cookies. If you need more than a couple of auth tokens, session ID's, and maybe a few layout/tracking cookies, you're living in the 90's.

Solution 4 - Ruby on-Rails

It's not a good idea to store a model object in the session.

Check out this railscast on this topic: http://railscasts.com/episodes/13-dangers-of-model-in-session?autoplay=true

It's a better practice to store the id (user's id in this case) inside the session. Then you won't have this problem.

(See Frederick Cheung comment above also).

Solution 5 - Ruby on-Rails

the error message clearly indicates the problem with cookie store size that's overflow.

Your sessions (by default in cookie) needs to be moved to Active record store or memcache store to fix this issue.

For Databased sessions:

config.action_controller.session_store = :active_record_store

You need to create the session table as below

rake db:sessions:create
rake db:migrate

OR

For Memcache sessions:

config.action_controller.session_store = :mem_cache_store

Also you need to setup a mem cache server and configure it as below:

config.cache_store = :mem_cache_store, 'localhost', '127.0.0.1:11211',
{:namespace => 'myapp123'}

Solution 6 - Ruby on-Rails

That error is because you are trying to serialize the user model When storing an object in a cookie, rails will use Marshal.dump which can produce a large amount of content since it's everything on the user record

Instead of storing the actual user record with session[:current_user] = user try just storing the users' ID then have a method a method to look up the user from that e.g.

def sign_in(user)
  ...
  session[:current_user_id] = user.id
end

def current_user
  @current_user ||= User.find(session[:current_user_id])
end

Solution 7 - Ruby on-Rails

This error appeared for me when I was running a specs. After updating Capybara from 1.x to 2.x. Just rake tmp:clear solved it.

Solution 8 - Ruby on-Rails

my problem was because of code

rescue StandardError => e
  flash[:error] = "Error was #{error.message}"
end

the error.message was too big

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionerogolView Question on Stackoverflow
Solution 1 - Ruby on-RailsAMIC MINGView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAlter LagosView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDavid HempyView Answer on Stackoverflow
Solution 4 - Ruby on-RailsZack XuView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMy GodView Answer on Stackoverflow
Solution 6 - Ruby on-RailscianmceView Answer on Stackoverflow
Solution 7 - Ruby on-RailsArtur79View Answer on Stackoverflow
Solution 8 - Ruby on-RailssrghmaView Answer on Stackoverflow