Christian Fazzini
Christian Fazzini

Reputation: 19713

How to store an array into a cookie?

How do you store a multi-array type of data into a cookie.

For example: [[1, 'foo'], [2, 'bar'], [3, 'foobar']]

I can get it to work with a single dimensional array as such:

cookies[:foobar] = { :value => cookies[:foobar] << ",1" }

and then do

cookies[:foobar].split(',').include?("1")

To verify that 1 exists inside the cookie. Not too sure how I can get around this with a multidimensional array

Upvotes: 10

Views: 3211

Answers (2)

4ndrew
4ndrew

Reputation: 16202

Serialize array into json and store to cookies.

Look at two methods:

ActiveSupport::JSON.encode(object)
ActiveSupport::JSON.decode(string)

Upvotes: 13

Frederick Cheung
Frederick Cheung

Reputation: 84114

The easiest is probably to use one of the serialisation methods rails/ruby provides such as YAML, marshalling or json.

Upvotes: 1

Related Questions