# Cookies problem

**URL:** <https://www.ruby-forum.com/t/cookies-problem/111542>\
**Category:** Rails\
**Created:** [August 8, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/cookies-problem/111542 "2007-08-08T12:55:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [August 8, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/cookies-problem/111542/1 "2007-08-08T12:55:09Z")

</div>

I seem to have some cookies (semi-)working. Sometimes I get the  
following error:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.[]

Sometimes I don’t.

My controller contains:

def prefs  
cookies.delete :runs  
cookies.delete :failures  
@values = params[:prefs]  
cookies[:runs] = { :value =\> @values[:runs]}  
cookies[:failures] = { :value =\> @values[:failures]}  
end

My view contains:

\<%= form\_tag :action =\> “prefs” %\>  
Number of runs to display: \<%= text\_field :prefs, :runs %\>  
  
Number of run failures to display: \<%= text\_field :prefs, :failures %\>  
\<%= submit\_tag ‘Save Preferences’ %\>

The error is being thrown up on the two lines beginning cookies[:runs]  
and cookies[:failures] in my controller.

Anyone any ideas why?

---

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [August 8, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/cookies-problem/111542/2 "2007-08-08T14:49:34Z")

</div>

> Sometimes I don’t.
> 
> My controller contains:
> 
> def prefs  
> cookies.delete :runs  
> cookies.delete :failures  
> @values = params[:prefs]  
> cookies[:runs] = { :value =\> @values[:runs]}  
> cookies[:failures] = { :value =\> @values[:failures]}  
> end

first off, from what the error says, You might have expected an instance  
of Array. and if you say it errors on the below lines:

@values = params[:prefs]  
cookies[:runs] = { :value =\> @values[:runs]}  
cookies[:failures] = { :value =\> @values[:failures]}

the problem is that @values should be an array, but it is not (i.e, the  
params[:prefs] is nil) - check to see if it is being passed correctly.

second off, not closley related to the problem, but for some reason it’s  
helped me before with cookies -  
add an expires opt as well:

:expires =\> Time.now + 10.days

the cookie can expire out, when you don’t want it too… it’s better to  
add an expire opt as well as the value (you can add a Time.now +  
10.years if you don’t really want it expiring …)

hth

---

<div class="post-metadata">

**Author:** ![nanouk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/da6949/32.png) [@nanouk](https://www.ruby-forum.com/u/nanouk)\
**Post date:** [August 8, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/cookies-problem/111542/3 "2007-08-08T18:10:23Z")

</div>

Shai R. wrote:

> > Sometimes I don’t.
> > 
> > My controller contains:
> > 
> > def prefs  
> > cookies.delete :runs  
> > cookies.delete :failures  
> > @values = params[:prefs]  
> > cookies[:runs] = { :value =\> @values[:runs]}  
> > cookies[:failures] = { :value =\> @values[:failures]}  
> > end
> 
> first off, from what the error says, You might have expected an instance  
> of Array. and if you say it errors on the below lines:
> 
> @values = params[:prefs]  
> cookies[:runs] = { :value =\> @values[:runs]}  
> cookies[:failures] = { :value =\> @values[:failures]}
> 
> the problem is that @values should be an array, but it is not (i.e, the  
> params[:prefs] is nil) - check to see if it is being passed correctly.
> 
> second off, not closley related to the problem, but for some reason it’s  
> helped me before with cookies -  
> add an expires opt as well:
> 
> :expires =\> Time.now + 10.days
> 
> the cookie can expire out, when you don’t want it too… it’s better to  
> add an expire opt as well as the value (you can add a Time.now +  
> 10.years if you don’t really want it expiring …)
> 
> hth

Well it seems to be passed correctly, as it works the first time and  
then fails any subsequent times!

I added the ‘expires’ option shortly after my post so i’m sure it’s not  
this causing the problem.

Anyone any more ideas?
