@session is getting reset?

I'm using r4rmusic (from "Ruby for Rails") as the basis for a hangman game, but I appear to have broken something. For some reason, @session is not retaining values for me.

At the end of the login method (user_controller.rb), I'm setting values into @session, then redirecting, as:

      puts ">>>>> redirecting" # TRACE       @session['foozle'] = 'bar'       if (@session['foozle'])         puts ">>>>> login: foozle = '#{@session['foozle']}'"       else         puts ">>>>> login: foozle is undefined"       end

      redirect_to :controller => 'main',                   :action => 'welcome'

This prints out

redirecting login: foozle = 'bar'

Then, in get_user (application.rb), I try to retrieve them:

    if (@session)       puts ">>>>> @session is defined in get_user"       puts ">>>>> class = #{@session.class}"

      if (@session['foozle'])         puts ">>>>> get_user: foozle = '#{@session['foozle']}'"       else         puts ">>>>> get_user: foozle is undefined"       end

This prints out

@session is defined in get_user class = CGI::Session get_user: foozle is undefined

I haven't a clue how to debug this. Help?

-r

Hi Rich,

Rich Morin wrote:

@session is not retaining values for me.

You don't say what version of Rails you're using, but if it's recent, the problem could be that '@session' is no longer a supported way of referencing the session hash. Use ':session' instead. The '@' prefix denotes an instance variable. Moving to :session reduces confusion.

hth,

Bill

@session and the other instance variables are deprecated but still supported. You’ll see warnings galore until you update your code, but no breakage. Use the session method (:session is a symbol, not a method call) rather than the @session instance variable.

Best, jeremy

Hi --

@session is not retaining values for me.

You don't say what version of Rails you're using, but if it's recent, the problem could be that '@session' is no longer a supported way of referencing the session hash. Use ':session' instead. The '@' prefix denotes an instance variable. Moving to :session reduces confusion.

@session and the other instance variables are deprecated but still supported. You'll see warnings galore until you update your code, but no breakage.

And sometimes after you update your code :slight_smile:

   $ for f in `find app -type f -not -path "*svn*"`;      do grep "@flash" $a; done    $ cd test/integration    $ ruby user_changes_profile_test.rb    Loaded suite user_changes_profile_test    Started    DEPRECATION WARNING: @flash is deprecated! Call flash. instead of    @flash..

   # etc.

This is on edge (r. 5271). I haven't dug into where it's coming from yet.

David