0

I have a Session class to store the currently logged user. It's something like that:

class Session
  attr_reader :user, :ip, :gef
  attr_accessor :branchid

  def initialize(user, ip, gef=nil)
    (...)
  end

  (...)

end

Then I have a controller method to change this branchid, so the user can access different things. The logged_user is a Session class that's being set as a helper method on the ApplicationController:

class ApplicationController < ActionController::Base

  helper_method :logged_user

  (...)

  def some_method_that_sets_logged_user_before_calling_change_branch
    self.logged_user = Session.new params[:user], params[:ip]
  end

  def change_branch
    logged_user.branchid = params[:branchid]
    redirect_to root_path
  end
end

That worked perfectly when I was using Ruby 1.9.3 with Rails 3.2. I'm now upgrading Ruby to 2.1.4, and this seems to have stopped working. I put a byebug in the middle of the change_branch method to see what's going on, and trying to alter logged_user directly via the console simply doesn't work.

When I type (in the server console, while debugging)

self.logged_user.branchid = 31

and then call

self.logged_user.branchid

it will return the old value, not 31.

Did anything in Ruby 2.0 or 2.1 change so this won't work anymore?

DickieBoy
  • 4,886
  • 1
  • 28
  • 47
Rodrigo Castro
  • 1,573
  • 14
  • 38
  • 1
    If `logged_user` is just an instance of the `Session` class then unless something else is going on you aren't mentioning, you are successfully setting the branchid and then redirecting which is going to blow away your `logged_user` object and give you a fresh one. You need to persist that change to the actual rails session if you want it remembered between redirects. – Alex Peachey Nov 04 '14 at 20:10
  • That makes sense, but then why did it work before? I recently started working on this code so I don't really know why they did it this way, but I used the system for a while and indeed it was working before. I'll think of a better way to to this though, thanks – Rodrigo Castro Nov 04 '14 at 20:15
  • how is `logged_user` implemented? – phoet Nov 04 '14 at 22:29

0 Answers0