Forum: Ruby on Rails undefined method `[]' for nil:NilClass

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aa33613fb12765fe505efea87844a12e?d=identicon&s=25 Craig White (Guest)
on 2007-01-19 16:29
(Received via mailing list)
I have an interesting situation and I am wondering if I can trap just
this specific error...

undefined method `[]' for nil:NilClass

which comes the user is trying to access a controller method which
assumes session variables that no longer exist because the session has
timed out.

I would rather bounce them to a static web page than toss the
application error in this case. Is this possible?

--
Craig White <craig@tobyhouse.com>
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-09-25 22:30
(Received via mailing list)
> application error in this case. Is this possible?
Yes...

begin
   #access the session that errors out
rescue
   redirect_to '/somewheresafe'
end

Or test it using nil? before calling [] on it...
This topic is locked and can not be replied to.