Listing values belonging to logged-in user

I saw a post on creating user Management Using Salted Hash Login
Generator. I followed the steps, but I cannot get it to work. I keep
getting a nil

the troublesome code is in the plunge_controller.rb:
def list
@plunge_pages, @plunges = paginate :plunges, :conditions => [‘user_id
= ?’, @session[‘user’].id]
end

This gives a “Called id for nil, which would mistakenly be 4 – if you
really wanted the id of nil, use object_id”

My plunge.rb model starts like this:
class Plunge < ActiveRecord::Base
belongs_to :user,
:class_name => “User”,
:foreign_key => “user_id”

and my user,rb like this:
class User < ActiveRecord::Base
has_many :plunges,
:class_name => “User”,
:foreign_key => “user_id”


I can get it to work by hardcoding the user id like this:
@plunge_pages, @plunges = paginate :plunges, :conditions => ‘user_id =
1’

Anyone who can help?

Jan A. wrote:

I saw a post on creating user Management Using Salted Hash Login
Generator. I followed the steps, but I cannot get it to work. I keep
getting a nil

the troublesome code is in the plunge_controller.rb:
def list
@plunge_pages, @plunges = paginate :plunges, :conditions => [‘user_id
= ?’, @session[‘user’].id]
end

This gives a “Called id for nil, which would mistakenly be 4 – if you
really wanted the id of nil, use object_id”

My plunge.rb model starts like this:
class Plunge < ActiveRecord::Base
belongs_to :user,
:class_name => “User”,
:foreign_key => “user_id”

and my user,rb like this:
class User < ActiveRecord::Base
has_many :plunges,
:class_name => “User”,
:foreign_key => “user_id”


I can get it to work by hardcoding the user id like this:
@plunge_pages, @plunges = paginate :plunges, :conditions => ‘user_id =
1’

Anyone who can help?

@session[‘user’] is returning nil. (You knew this, right?) The question
is why. Here are some things to check:

  • Are sessions working? They usually require some configuration, esp.
    if they are stored in the database. If you have just dropped this
    library into a new Rails Application check this out first.

  • Is the session being set properly? Check your controllers. If you
    cannot find a place where @session[‘user’] = you have a
    problem (Note: this may have come with the library code)

  • Is the browser/test case your using preserving sessions between calls?
    I cannot tell you the number of times a stale browser cache in Fire Fox
    or a test case that is not session aware has blown my whole day.

As a final thought, a lot of these data persistence problems are very
difficult to track down because there is no way to trace the execution
path through the program. We MUST wait for the client to make a second
request with the right data. To help me with this I have a debug
template that I use for all my pages. The template contains a button at
the bottom that will pop up a window which lists debug(@user),
debug(params), debug(session), and debug(flash). Very helpful for these
types of things.

Hi John,

Thanks for taking the time to help me. Your comments put me on the right
track. I did some hours of debugging and decided to rewrite the thing
as:
def list
@plunges = Plunge.find:all, :conditions => [“user_id = ?”,
session[:user_id]]
end

My login controller looks like this:
def login
session[:user_id] = nil
if request.post?
user = User.authenticate(params[:name], params[:password])
if user
session[:user_id] = user.id

No matter what I do, I cannot get @session[‘user’].id to be anything
other than nil, so I guess that session[:user_id] is an ok replacement
:slight_smile:

Anyway, Thanks again for your help.
Jan

John M. wrote:

Jan A. wrote:

I saw a post on creating user Management Using Salted Hash Login
Generator. I followed the steps, but I cannot get it to work. I keep
getting a nil

the troublesome code is in the plunge_controller.rb:
def list
@plunge_pages, @plunges = paginate :plunges, :conditions => [‘user_id
= ?’, @session[‘user’].id]
end

This gives a “Called id for nil, which would mistakenly be 4 – if you
really wanted the id of nil, use object_id”

My plunge.rb model starts like this:
class Plunge < ActiveRecord::Base
belongs_to :user,
:class_name => “User”,
:foreign_key => “user_id”

and my user,rb like this:
class User < ActiveRecord::Base
has_many :plunges,
:class_name => “User”,
:foreign_key => “user_id”


I can get it to work by hardcoding the user id like this:
@plunge_pages, @plunges = paginate :plunges, :conditions => ‘user_id =
1’

Anyone who can help?

@session[‘user’] is returning nil. (You knew this, right?) The question
is why. Here are some things to check:

  • Are sessions working? They usually require some configuration, esp.
    if they are stored in the database. If you have just dropped this
    library into a new Rails Application check this out first.

  • Is the session being set properly? Check your controllers. If you
    cannot find a place where @session[‘user’] = you have a
    problem (Note: this may have come with the library code)

  • Is the browser/test case your using preserving sessions between calls?
    I cannot tell you the number of times a stale browser cache in Fire Fox
    or a test case that is not session aware has blown my whole day.

As a final thought, a lot of these data persistence problems are very
difficult to track down because there is no way to trace the execution
path through the program. We MUST wait for the client to make a second
request with the right data. To help me with this I have a debug
template that I use for all my pages. The template contains a button at
the bottom that will pop up a window which lists debug(@user),
debug(params), debug(session), and debug(flash). Very helpful for these
types of things.