I have a helper module:
module SessionsHelper
@current_user
@current_session
def current_user
@current_user = User.find(cookies[:user_id]) if(cookies[:user_id])
end
def current_session
@current_session = Session.find_by_secret_key(cookies[:session_secret_key])
end
def current_user=(user)
@current_user = user
end
def current_session=(session)
@current_session = session
end
end
and a controller:
class SessionsController < ApplicationController
include SessionsHelper
respond_to :json
def create
u = User.find_by_ip(request.remote_ip)
u = User.create({:ip => request.remote_ip}) unless u
s = Session.create
s.admin = u
s.save!
send(:current_user=,u)
send(:current_session=,s)
respond_with s
end
end
notice how I have to set the current user with the :send
method, because calling current_user=u
directly in the controller will not do it.