Announcing:

LessMoney Conference will be June 7th in Tampa! Register today and make us smile super big!

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.

1 Comment

Jason Seifer
Jason Seifer said on August 06, 2008

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

Leave a Comment

About Steven
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.

You Should...

Follow Steven on Twitter
Friend Steven on Facebook
Subscribe
LessEverything Copyright 2011 LessEverything.com
We don't like footers, they're kinda boring