title: Why does current_user session become nil when updating a user?
body: <p>I'm using Devise and CanCan for user authentication and administrating roles restricting access to parts of my Rails 4 app for certain users.</p>
  
  <p>I've run into some problems with updating a user. The update works fine and the user object in the db get updated as it should, but my user session is lost on the following <code>redirect_to</code> my user show action. <code>current_user</code> becomes <code>nil</code> which means that CanCan restricts the access to the user show action.</p>
  
  <p>Why does <code>current_user</code> become <code>nil</code> after update, when this does not happen on other actions (e.g create, destroy etc.)?</p>
  
  <p>These are the devise settings in my user model:</p>
  
  <pre><code>devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable, :authentication_keys =&gt; [:login]
  </code></pre>
  
  <p>This is my users_controller.rb's update method:</p>
  
  <pre><code>class UsersController &lt; ApplicationController
  
    load_and_authorize_resource
    before_filter :authenticate_user!
  
    def update
      @user = User.find(params[:id])
      if params[:user][:password].blank?
          params[:user].delete(:password)
      end
  
      respond_to do |format|
        if @user.update_attributes(user_params)
          format.html { redirect_to user_path, :notice =&gt; 'User was successfully updated.' }
          format.json { head :ok }
        else
          format.html { render :action =&gt; "edit" }
          format.json { render :json =&gt; @user.errors, :status =&gt; :unprocessable_entity }
        end
      end
    end
  end
  </code></pre>
  
  <p>And this is my ability.rb file:</p>
  
  <pre><code>class Ability
    include CanCan::Ability
  
    def initialize(user)
      user ||= User.new # guest user (not logged in)
      if defined?(user.role_id)
        if user.role? :admin, user.role_id
          can :manage, :all
        elsif user.role? :hauler, user.role_id
          can :manage, [User,Trip,Invoice], user_id: user.id.to_s
        else
          can :create, :Trip
        end
      end
    end
  end
  </code></pre>