Flash[:error] and Cookie Sessions

Written by on Aug 6 2008

We still have a few apps that store the errors collection of an ActiveRecord object in the flash[:error] hash. This always worked fine, but since switching to cookie store, sometimes we get an exception because the cookie data is greater than 4KB.

What happens is the ActiveRecord::Base object is stored in the ActiveRecord::Errors object. If this object is too big, the exception is raised.

I monkey patched the FlashNow object to nil the @base ivar in flash.now. Here is the code:

class ActionController::Flash::FlashNow def []=(k, v) newv = v.clone newv.instancevariableset(:@base, nil) if v.isa?(ActiveRecord::Errors) @flash[k] = newv @flash.discard(k) v end end

class ActionController::Flash::FlashHash def []=(k, v) newv = v.clone newv.instancevariableset(:@base, nil) if v.isa?(ActiveRecord::Errors) keep(k) super(k, newv) end end

 

This takes care of the problem without affecting any other part of the app. Just drop this code in an initializer and bob’s your uncle.
 

Meet
Steven

Hi I'm Steven,

I wrote the article you're reading... I lead the developers, write music, used to race motorcycles, and help clients find the right features to build on their product.

Get Blog Updates