Sessions

Okay I put this into my controller

if @session['layout'] == 1
  layout 'main'
elsif @session['layout'] == 2
  layout 'main2'
else
  layout 'main'
end

In the app.

@session['layout'] = 1 if @session['layout'] == nil

it says ‘You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]’
What am I doing wrong?

Mohammad wrote:

Okay I put this into my controller

if @session['layout'] == 1
>   layout 'main'
> elsif @session['layout'] == 2
>   layout 'main2'
> else
>   layout 'main'
> end

In the app.

@session['layout'] = 1 if @session['layout'] == nil

it says ‘You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]’
What am I doing wrong?

Try using “session[:key]” instead of “@session[‘key’]”, using the
instance variable directly is deprecated.

Also what line exactly is causing the error? with so many references of
the session hash it would be good know exactly which one is crashing it.
Hard to troubleshoot otherwise.

Lastly a slightly cleaner way to do this:

session[:layout] = 1 if session[:layout] == nil

Would be this:

session[:layout] = 1 if session[:layout].nil?

Or even better:

session[:layout] = 1 unless session[:layout]

But in my opinion the best way is:

session[:layout] ||= 1

On 6/23/06, Alex W. [email protected] wrote:

end
[/code]

if @session[‘layout’] has not been defined or initialized you will get
this
error. So, before you test @session[‘layout’] == 1 you need to check
for
@session[‘layout’].nil?

Something like:

if @session[‘layout’].nil?
layout = ‘main’
#and initialize @session[‘layout’] to something for later checks
elsif @session[‘layout’] == 1
layout = ‘main’
elsif @session[‘layout’] == 2
layout = ‘main2’
end

One question…instead of storing the numbers why not store ‘main’ and
‘main2’ in @session[‘layout’]?

Alex W. wrote:

Also what line exactly is causing the error? with so many references of
the session hash it would be good know exactly which one is crashing it.
Hard to troubleshoot otherwise.

I change all of them to @session[‘layout’] but now,
[error]Symbol as array index[/error]

Mohammad wrote:

Alex W. wrote:

Also what line exactly is causing the error? with so many references of
the session hash it would be good know exactly which one is crashing it.
Hard to troubleshoot otherwise.

I change all of them to @session[‘layout’] but now,
[error]Symbol as array index[/error]

“session” should be a hash not an array. Are you overriding the sesion
object somewhere? What I mean is you should never do:

session = {:foo => ‘bar’}

but instead do

session[:foo] = ‘bar’

if you let rails manage the creation of the session, it should be a
hash, not an array.