# Cookies & gsub?

**URL:** <https://www.ruby-forum.com/t/cookies-gsub/77509>\
**Category:** Rails\
**Created:** [March 6, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/cookies-gsub/77509 "2006-03-06T23:55:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [March 6, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/cookies-gsub/77509/1 "2006-03-06T23:55:13Z")

</div>

I’m looking to store the current params into a/some cookie(s).

This works:

@params.each\_pair do |this\_index, this\_param|  
cookies[this\_index] = this\_param  
end

Except, I get session-only cookies. This doesn’t work:

@params.each\_pair do |this\_index, this\_param|  
cookies[this\_index] = {  
:value =\> this\_param,  
:expires =\> 30.days.from\_now  
}  
end

Specifcally, I get the error:

> private method `gsub’ called for [“Sonic”, “0”]:Array

I’m not entirely sure I understand why one of these works, but the other  
doesn’t. Can someone point me in the right direction here?

---

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [March 7, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/cookies-gsub/77509/2 "2006-03-07T16:14:55Z")

</div>

I’ve been poking around with this. I’m starting to think this may be a  
minor bug in the cookie module… Here’s what I’ve got now:

I’m converting an older app from PHP to Rails. The users makes a few  
choices and I display/filter the data to match their choices. I want  
to store their choices in a cookie, so they remain “stuck” the next  
time the visitor returns to the site.

The problem: Storing a Hash in the cookie with an expiration gives a  
“NoMethodError” – not finding “gsub” on the Hash/Array (?).

More specifically:  
The user makes a choice as to which book to search in, the source list  
comes from the database, and uses abbreviations to track which source  
is selected. I converted this almost straight from PHP:

\<% @optional\_sources.each do |this\_source| -%\>  
\<input type=“checkbox” name=“add\_sources[]”  
value="\<%=this\_source.abbrev%\>" id="\<%=this\_source.abbrev%\>"\<%=  
‘checked=“checked”’ if  
@cookies[‘add\_source’].index(this\_source.abbrev) %\> /\> \<%=this\_source.name%\>  
  
\<% end -%\>

This returns a @params array like this:

@params[‘add\_sources’] = [‘ABBREV1’, ‘ABBREV2’, ‘ABBREV3’]

Of course, this isn’t a very “Railsian” approach. So I implemented the  
next set of choices (let’s say, choosing a city) using the check\_box  
helper:

**Cities:**

\<% cities = %w{ Toronto Chicago } cities.each do |this\_city| %\>- \<%= check\_box ("cities", this\_city) %\> \<%=this\_city%\> 
\<% end -%\>

Which produces a @params hash like this:

@params[‘cities’][‘Toronto’] = 0  
@params[‘cities’][‘Chicago’] = 1

Or, to put it another way, like this:

@params[‘cities’] = { ‘Toronto’ =\> 0, ‘Chicago’ =\> 1 }

The problem (at long last):  
In the first case (sources), this works fine:

cookies[:sources] = { :value =\> @params[:add\_sources], :expires =\>  
30.days.from\_now }

In the second case (cities), this DOES work:

cookies[:cities] = @params[:cities]

BUT, creates a this-session-only cookie. Trying to set an expiration:

cookies[:sources] = { :value =\> @params[:cities], :expires =\>  
30.days.from\_now }

… produces the gsub error.

---

<div class="post-metadata">

**Author:** ![Josh\_on\_Rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@Josh\_on\_Rails](https://www.ruby-forum.com/u/Josh_on_Rails)\
**Post date:** [April 5, 2006, 3:00am UTC](https://www.ruby-forum.com/t/cookies-gsub/77509/3 "2006-04-05T03:00:24Z")

</div>

in the rails book it says cookie values can only have string types  
otherwise  
you’ll run into the gsub error. Agile Rails page 312

you might want to stringify your hash and store it in the cookie. Then  
when  
extracting it, parse it back into your hash object.
