Overflow of cookies in rails?

ActionDispatch :: Cookies :: CookieOverflow in UsersController # create

I have this error when I try to open a page. I do not know how to debug this error. Do you have any suggestions on this issue?

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 
+102
ruby-on-rails
Feb 27 2018-12-12T00:
source share
6 answers

You have a 4kb limit on what you can store in cookies, and when Rails converts your object into text for writing to a cookie, it is probably beyond this limit.

Ruby on Rails ActionDispatch::Cookies::CookieOverflow

Therefore, this CookieOverflow error CookieOverflow .

The easiest way to resolve this issue: you need to change your session_store and not use cookie_store . You can use the active_record_store example.

Here are the steps

  • Generate a migration creating a session table

     rake db:sessions:create 
  • Start the migration

     rake db:migrate 
  • Change config/initializers/session_store.rb from

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

    to

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

After completing the three steps, restart the application. Rails will now use the session table to store session data, and you will not have a 4kb limit.

+150
Feb 27 2018-12-12T00:
source share

For the function :active_record_store work in Rails 4/5, you must add activerecord-session_store to your Gemfile

 gem 'activerecord-session_store' 

then run the migration generator:

 rails generate active_record:session_migration rake db:migrate 

Finally, set up the session store in config/initializers/session_store.rb :

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

UPDATE:

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

+74
02 Sep '13 at 23:13
source share

The error message clearly indicates a problem with the size of the cookie that is overflowing.

Your sessions (by default in the cookie) need to be transferred to the active storage or memcache storage to fix this problem.

For database sessions:

 config.action_controller.session_store = :active_record_store 

You need to create a session table below

 rake db:sessions:create rake db:migrate 

OR

For Memcache sessions:

 config.action_controller.session_store = :mem_cache_store 

You also need to configure the mem cache server and configure it as shown below:

 config.cache_store = :mem_cache_store, 'localhost', '127.0.0.1:11211', {:namespace => 'myapp123'} 
+10
Jan 15 '13 at 10:52
source share

It is not recommended that you store the model object in a session.

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

It is best to store id (user id in this case) inside the session. Then you will not have this problem.

(See also commentary by Frederick Chung).

+9
Mar 04 '13 at 17:19
source share

If you see this, make sure you do not blow up some session data. In my case, these were thousands of messages that were uploaded to a flash message. Just saying it.

+7
Jul 14 '17 at 20:30
source share

This error appeared to me when I ran the specs. After upgrading Capybara from 1.x to 2.x. Just rake tmp: clear solved this.

+1
May 08 '13 at 15:20
source share



All Articles