title: Ruby on Rails Remember Me Log In Not Working
body: <p>I am following the following tutorial:</p>
  
  <p><a href="http://railscasts.com/episodes/274-remember-me-reset-password" rel="nofollow">http://railscasts.com/episodes/274-remember-me-reset-password</a></p>
  
  <p>And have just tried to add the 'remember me' functionality, so only up to around 4 minutes of the tutorial.</p>
  
  <p><strong>Everything seems fine but once I try to log in it just goes back to the home page and no one is logged in.</strong></p>
  
  <p>User.rb:</p>
  
  <pre><code>before_create { generate_token(:auth_token) 
  
  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column =&gt; self[column])
  end
  </code></pre>
  
  <p>sessions_controller:</p>
  
  <pre><code>def create
      user = User.find_by email: params[:email]
      if user and user.authenticate params[:password]
          # 'logging in' is performed by saving a user's id in the session variable
          #session[:user_id] = user.id
          #cookies.permanent[:auth_token] = user.auth_token
          if params[:remember_me]
            cookies.permanent[:auth_token] = user.auth_token
          else
            cookies[:auth_token] = user.auth_token
          end
          # redirect to last page or root_path (products/index.html.erb')
          redirect_to root_path
      else
          flash.now.notice = "Invalid user/password"
          render :new
      end
  end
  
  
  def destroy     
      # the act of logging out is performed by simply setting the key (:user_id)      
      # in the session hash to a value of nil (nothing)                   
      #session[:user_id] = nil
      cookies.delete(:auth_token)     
      # redirect to root_path (products/index.html.erb') 
      redirect_to root_path 
  end
  </code></pre>
  
  <p>application_controller:</p>
  
  <pre><code>def authenticate_user
      # if session[:user_id]
          # @current_user = User.find session[:user_id]
      # else
          # @current_user = nil
      # end
      if cookies[:auth_token]
          @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) 
      else
          @current_user = nil
      end
  end
  
  def logged_in?      
      not session[:user_id].nil?  
  end 
  </code></pre>
  
  <p>views/sessions/new.html.erb:</p>
  
  <pre><code>&lt;%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" =&gt; true %&gt;
  &lt;!-- DRAW HTML CONTENT FOR PAGE --&gt;
  &lt;%= form_tag sessions_create_path, :method =&gt; :get do %&gt;&lt;div id='wrapper'&gt;
      &lt;div id="contentWrapper"&gt;
          &lt;div id="contentWrapper"&gt;
              &lt;h1 class="page_header"&gt;THOR CINEMAS:&lt;div class="file_selector"&gt;&lt;span class="darker"&gt;LOG IN&lt;/span&gt;&lt;/div&gt;&lt;/h1&gt;
              &lt;div id="content"&gt;
                  &lt;!-- FULL PAGE CATEGORY CONTAINER --&gt;
                      &lt;div class='category'&gt;
                          &lt;div class='large_panel'&gt;
                             &lt;%= image_tag "thor_hammer.jpg",:size =&gt; "900x250" %&gt;
                             &lt;h1&gt;Log IN:&lt;/h1&gt;&lt;table&gt;
                             &lt;tr&gt;
                                  &lt;td&gt;
                                      &lt;table&gt;
                                          &lt;tr&gt;
                                              &lt;td width="300px"&gt;
                                                  &lt;div class='info'&gt;
                                                      &lt;%= label_tag :email,'EMAIL:' %&gt;
                                                  &lt;/div&gt; 
                                              &lt;/td&gt;
                                              &lt;td&gt;
                                                 &lt;div class='info'&gt;
                                                     &lt;%= label_tag :password, 'PASSWORD:' %&gt;
                                                 &lt;/div&gt; 
                                             &lt;/td&gt;
                                             &lt;td&gt;&lt;/td&gt;
                                             &lt;td&gt;&lt;/td&gt;
                                         &lt;/tr&gt;
  
                                         &lt;tr&gt;
                                                &lt;td style="padding-right:45px"&gt;&lt;%= text_field_tag :email, params[:email] , :autofocus =&gt; true, :size =&gt; 42 %&gt; &lt;/td&gt;
                                                &lt;td style="padding-right:45px"&gt;&lt;%= password_field_tag :password, params[:password], :size =&gt; 42 %&gt;&lt;/td&gt;
                                                 &lt;div class="field"&gt;
                                                    &lt;%= check_box_tag :remember_me, 1, params[:remember_me] %&gt;
                                                    &lt;%= label_tag :remember_me %&gt;
                                                  &lt;/div&gt;
                                         &lt;/tr&gt;
  
                                      &lt;/table&gt;&lt;br&gt;&lt;p style="color:red"&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;
                                  &lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;table&gt;
                                          &lt;tr&gt;
                                              &lt;td&gt; 
                                                  &lt;div class='info'&gt;
                                                      OPTIONS:
                                                  &lt;/div&gt; 
                                              &lt;/td&gt;
                                              &lt;td&gt;
  
                                              &lt;/td&gt;
                                              &lt;td&gt;&lt;/td&gt;
                                              &lt;td&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
  
                                          &lt;tr&gt;
                                              &lt;td&gt;&lt;%= submit_tag "Login" %&gt;&lt;/br&gt;&lt;/td&gt;
                                              &lt;td&gt;&lt;%= link_to "Register", new_user_path %&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
  
                                      &lt;/table&gt;&lt;br&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                          &lt;/table&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;  
      &lt;/div&gt;
  &lt;/div&gt;&lt;% end %&gt;
  &lt;/body&gt;   
  &lt;/html&gt;
  </code></pre>
  
  <p>Films_controller:</p>
  
  <pre><code>def index
      @films = Film.all
      if logged_in?
          @colour = Perference.find_by(user_id: session[:user_id]).colour
      end
  end
  </code></pre>
  
  <p>Can someone please tell me what the problem is.</p>