Storing a hash in a cookie

Hi guys. I’m trying to do the following:

value = {:number => 1, :num_list => [1,2,3]}

cookies[:some_name] = {:value => value,
:expires => 1.day.from_now}

I’m getting the gsub error. How do I make my ‘value’ hash a string and
then decode it back to its original Hash type? Any example code would
be helpful here.

thanks.

Sam,

value = {:number => 1, :num_list => [1,2,3]}
cookies[:some_name] = {:value => value,
:expires => 1.day.from_now}
I’m getting the gsub error.

I typed your code in the console (see below), and it works fine!
Typo?

Alain

value={:number => 1, :num_list => [1,2,3]}
=> {:num_list=>[1, 2, 3], :number=>1}

app.cookies[:test]={:value => value, :expires => 1.day.from_now}
=> {:value=>{:num_list=>[1, 2, 3], :number=>1}, :expires=>Wed Nov 29
15:57:29 CET 2006}

app.cookies[:test]
=> {:value=>{:num_list=>[1, 2, 3], :number=>1}, :expires=>Wed Nov 29
15:57:29 CET 2006}

app.cookies[:test][:value].class
=> Hash

That looks like something that would only work in a test environment

Guys, this is definitely not working in Rails and other people have
gotten
gsub errors for this. Can somebody tell me how to safely store the
value
into the cookie as a string and then retrieve that value back into a
Hash?

Thanks.

On 11/29/06, Sam D. [email protected] wrote:

Guys, this is definitely not working in Rails and other people have gotten
gsub errors for this. Can somebody tell me how to safely store the value
into the cookie as a string and then retrieve that value back into a Hash?

Of course it won’t work. Even if it let you set the value, when you
later retrieve the cookie it’s just a string, and you can’t convert it
back into a hash. Just marshal it then base64 encode it, then
decode/unmarshal it when you read it back. Not that I suggest doing
that, I think it’s crazy, but it will work.

Chris