Hello -
I’m trying to have an account information and link bar at the top of the
screen, i.e:
[email protected] | My Profile | Logout
This bar should be up there even when the user is on a cached page (the
app I’m working on has a lot of database-intensive screens, which
luckily are well-suited to page caching). So, I set up a before filter
in the application controller, like so:
before_filter :set_user_info_cookie
private
def set_user_info_cookie
cookies[:user_info] = { :value => current_user.email, :expires =>
1.hour.from_now } if current_user
cookies.delete :user_info unless current_user
end
(I’m using Authlogic for authentication)
Then I use jQuery to look for the “user_info” cookie - if it exists, it
assumes the user is logged in and puts the email address and whatnot at
the top of the screen. This is the only thing the cookie is used for, so
security’s not an issue (I don’t think) and it’s left in clear text.
There are two problems with my setup:
#1: “cookies.delete :user_info unless current_user” doesn’t work. In
fact, I can’t figure out how to destroy that cookie at all. When I use
the debugger, and then call cookies.delete [:user_info], I get the
following array with two strings:
[“user_credentials=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT”,
“user_info=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT”]
If I look at the cookies hash after that, it looks exactly the same as
it did before I tried to delete the user_info cookie. Looking at the
localhost cookies in Firefox, the user_info cookie is still there.
#2: I’ve just set up one controller specifically to serve static pages
(which will also be page cached, but I want to use rails to make sure
their layout matches the rest of the site). The controller actions are
empty, and the views are still very simple, but whenever I try to access
one of the static pages I get the following error, stemming from my
set_user_info_cookie method:
You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
C:/Ruby/lib/ruby/gems/1.8/gems/authlogic-2.0.14/lib/authlogic/session/cookies.rb:101:in
cookie_credentials' C:/Ruby/lib/ruby/gems/1.8/gems/authlogic-2.0.14/lib/authlogic/session/cookies.rb:106:in
persist_by_cookie’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:178:in
send' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:178:in
evaluate_method’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:166:in
call' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:93:in
run’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:92:in
each' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:92:in
send’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:92:in
run' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/callbacks.rb:276:in
run_callbacks’
C:/Ruby/lib/ruby/gems/1.8/gems/authlogic-2.0.14/lib/authlogic/session/callbacks.rb:78:in
persist' C:/Ruby/lib/ruby/gems/1.8/gems/authlogic-2.0.14/lib/authlogic/session/persistence.rb:55:in
persisting?’
C:/Ruby/lib/ruby/gems/1.8/gems/authlogic-2.0.14/lib/authlogic/session/persistence.rb:39:in
find' C:/Ruby/Projects/myapp/app/controllers/application_controller.rb:22:in
current_user_session’
C:/Ruby/Projects/myapp/app/controllers/application_controller.rb:27:in
current_user' C:/Ruby/Projects/myapp/app/controllers/application_controller.rb:14:in
set_user_info_cookie’
Looking at cookies.rb at github:
Things are failing on the line:
controller.cookies[cookie_key] &&
controller.cookies[cookie_key].split(“::”)
So it seems that controller.cookies is nil. I feel like this must be
related to my other problem, but I don’t know how.
Does anyone have any ideas or suggestions? Sorry this was so long, but I
suppose, better too much information than too little.
Thanks so much!
Chris