title: get session data in rails
body: <p>I'm new to rails coming from php, and I'm learning rails 3 with mongodb.</p>
  
  <p>I have got to the point where I have an html form post text to mongo, and i can create and login as a user.</p>
  
  <p>In the user login, i create a session with as created by nifty authenticate</p>
  
  <pre><code>session[:user_id] = user.id
  </code></pre>
  
  <p>now I'm trying to get that user_id from the session when I create a new post so that the post is connected to the user.</p>
  
  <p>in my posts_controller, I have the create function as follows.</p>
  
  <pre><code>def create
      @post = Post.new(params[:post])
      respond_to do |format|
        if @post.save
          format.html { redirect_to(@post, :notice =&gt; 'Post was successfully created.') }
          format.xml  { render :xml =&gt; @post, :status =&gt; :created, :location =&gt; @post }
        else
          format.html { render :action =&gt; "new" }
          format.xml  { render :xml =&gt; @post.errors, :status =&gt; :unprocessable_entity }
        end
      end
  end
  </code></pre>
  
  <p>however, i believe I need to pass the user_id in here, or maybe it would be better to put it in my model.</p>
  
  <p>But attempting any sort of </p>
  
  <pre><code>@user_id = session[:user_id]
  </code></pre>
  
  <p>hasn't returned anything meaningful. Am I going about this the wrong way?</p>
  
  <p>I've seen posts which describe storing sessions in the database, but I don't really see why this should be necessary for something as simple as just getting the currently logged-in user_id.</p>