title: Ruby rails - session management
body: <p>I am trying to manage session of users after authentication with ldap. The authentication part works very well, but can't seem to generate the session id. I get "undefined method `id' for true:TrueClass" error. </p>
  
  <p>Model - </p>
  
  <pre><code>def self.Authenticate(login, pass)
    user = find_by_user_id(login)
    if user
      user
    else
      return false
    end
    conn = Net::LDAP.new(
      :host =&gt; SERVER,
      :port =&gt; PORT,
      :base =&gt; BASE,
      :auth =&gt; { 
        :username =&gt; "#{login}@#{DOMAIN}",
        :password =&gt; pass,
        :method =&gt; :simple 
      }
    )
    if conn.bind
      return true
    else
      return false
    end
  rescue Net::LDAP::LdapError =&gt; e
    return false
  end    
  </code></pre>
  
  <p>Controller -    </p>
  
  <pre><code>def create
    user = User.Authenticate(params[:user_id], params[:password])
  
    if user
      session[:user_id] = user.id
      redirect_to theapp_url, :notice =&gt; "Logged in!"
    else
      flash.now.alert = "Invalid email or password"
      render "new"
    end
  end
  </code></pre>
  
  <p>View - </p>
  
  <pre><code>&lt;%= form_tag sessions_path do %&gt;
    &lt;p&gt;
      &lt;%= label_tag :Username %&gt;&lt;br /&gt;
      &lt;%= text_field_tag :user_id, params[:user_id] %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= label_tag :Password %&gt;&lt;br /&gt;
      &lt;%= password_field_tag :password %&gt;
    &lt;/p&gt;
    &lt;p class="button"&gt;&lt;%= submit_tag "Log in" %&gt;&lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The error - </p>
  
  <pre><code>NoMethodError in SessionsController#create
  
  undefined method `id' for true:TrueClass
  Rails.root: /myapp
  
  Application Trace | Framework Trace | Full Trace
  app/controllers/sessions_controller.rb:12:in `create'
  Request
  
  Parameters:
  
  {"utf8"=&gt;"✓",
   "authenticity_token"=&gt;"OmzCrLHR1t/xfIXNcEzy2NCGfVpEKSyI4OZfqpPEFNw=",
   "user_id"=&gt;"admin",
   "password"=&gt;"[FILTERED]",
   "commit"=&gt;"Log in"}
  </code></pre>