Read more

Do not try to "slice" on an ActionController::CookieJar

Arne Hartherz
July 15, 2013Software engineer at makandra GmbH

The cookies object in your controllers and views is a ActionController::CookieJar and even though that class inherits from Hash and often behaves like one, you can not call slice on it to select only a subset of cookies. Why? Because Hash#slice calls self.class.new -- which in case of a CookieJar just won't work.

Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

Unfortunately, you can't even say cookies.to_hash and slice on that, just because CookieJar#to_hash is inherited from Hash and will just return self. Bummer.

You need to do it yourself, for example by using collect_hash:

my_keys = %w[ foo bar baz ]
my_cookies = my_keys.collect_hash { |key| [ key, cookies[key] ] }
Posted by Arne Hartherz to makandra dev (2013-07-15 15:13)