Topic: Session variable changing value

I'm using this method to log users in:

def verify_login
  @user = User.find_by_nick(params[:user][:nick])
  if @user and @user.password == params[:user][:password]
    session[:uid] =
    flash[:notice] = "Hello, #{@user.first_name}. Welcome to RaceBucket."
    redirect_to :controller => "user", :action => "mybucket"
    flash[:notice] = "Invalid login; please try again."
    redirect_to :controller => "user", :action => "login"

This works fine. When I log in I'm redirected to the right page on which I'm displaying the session variable and it's correct. That is, it's storing the proper ID of the user.

I also have this in my application.rb to lookup the user's details on every page load if one exists:

before_filter :identify
def identify
  if session[:uid]
    @currentuser = User.find(session[:uid])

Here's the weird part. When I log in, the session variable is set correctly (11 in my testing). As soon as I hit refresh or navigate to another page the session variable gets set to '1'.

I'm not doing this explicitly anywhere that I can see.

Any ideas?

Re: Session variable changing value


I don't see anything right off the bat that jumps out at me and screams "wrong!".  So, you're going to have to do a bit of further debugging.

Why not try breaking out the breakpointer or adding a few "current session value: #{session[:uid]}" to the beginning / end of the application entrance/exit points.

At the very least, it will help you narrow down where it's happening at!

Ruby Rockstars - Ruby Jobs for all!

Re: Session variable changing value

Just to ask the obvious, is cookies enabled in the web browser settings? I would keep an eye on the session key in your browser cookies and see if it changes between requests.

Railscasts - Free Ruby on Rails Screencasts