Cookies problem

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?

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

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?