Flash[:error] and Cookie Sessions
written by Steven on August 06, 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)
new_v = v.clone
new_v.instance_variable_set(:@base, nil) if v.is_a?(ActiveRecord::Errors)
@flash[k] = new_v
@flash.discard(k)
v
end
end
class ActionController::Flash::FlashHash
def []=(k, v)
new_v = v.clone
new_v.instance_variable_set(:@base, nil) if v.is_a?(ActiveRecord::Errors)
keep(k)
super(k, new_v)
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.
Learn how LessEverything built their consultancy to over $1,000,000 annual revenue at LessMoney Conference, June 7th in Tampa Florida. Each attendee will get early access to our upcoming ebook as well.
Leave a Comment
About Steven
Popular Articles
Subscribe

Steven Bristol has written code for the past 20 years. He like green vegetables and kittens, oh and butterflies too. He loves to throw ninja stars at his enemies.

1 Comment
That’s awesome — a really creative work-around. You should try and get something like this in core.