title: How to access session variables across multiple controllers in Rails?
body: <p>I have implemented a facebook connect for my application. So when a user logs in I wish to store his facebook id in the session and use it across multiple controllers and views. I tried implementing this . I also referred to several similar questions answered in stackoverflow . However something seems wrong. I set the facebook id in the session in the UserController . I am not able to access it in the EventsController.</p>
  
  <p>The flow is :  When the user logins in for the first time . The fbinsertupdate is called. And then the page redirects to events#index page.</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
   def self.current=(u)
  @current_user = u
  end
  
  def self.current
  @current_user
  end
  
  
  end
  
  
  
  
  
  class ApplicationController &lt; ActionController::Base
    protect_from_forgery
  
  
  
    # Finds the User with the ID stored in the session with the key
    # :fb_user_id 
  
    def current_user
      User.current  ||= session[:fb_user_id] &amp;&amp;
        User.find(session[:fb_user_id])
    end
  
  
  end
  
  
  
  
  class UsersController &lt; ApplicationController
  
  
  
    def fbinsertupdate  #Facebook connect. Insert new user or update existing user
  
  
      @user = User.find_or_create_by_fbid(params[:fbid]) 
      @user.update_attributes(params[:user])
      session[:fb_user_id] = @user.fbid  # storing the facebook id in the session
      render :nothing =&gt; true
  
  
  
  
    end
  
  
  
  end
  
  
  class EventsController &lt; ApplicationController
  
    def index
       @events = Event.all
       @current_user= User.current
      logger.debug "The current user is  "+@current_user.name  # This fails . Says the class in nil
  
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml =&gt; @events }
      end
    end
  
  
  end
  </code></pre>