title: Can't delete session properly in rails
body: <p>Following the ruby on rails tutorial (<a href="https://www.railstutorial.org/book/log_in_log_out" rel="nofollow">https://www.railstutorial.org/book/log_in_log_out</a>). The logout feature isn't working. I generated a 'sessions' controller and defined a method destroy to 'log_out' the 'current_user' which is an instance variable and is changed to nil when the log_out method is called. 'log_out' method in the sessions_helper.rb.</p>
  
  <pre><code>module SessionsHelper
  def log_in(user)
      session[:user_id] = user.id
  end
  
  def remember(user)
    user.remember 
    cookies.permanent[:remember_token] = user.remember_token
    cookies.permanent.signed[:user_id] = user.id
  end
  
  def current_user
      if (user_id = session[:user_id])
          @current_user ||= User.find_by(id: session[:user_id])
      elsif (user_id = cookies.signed[:user_id])
          user = User.find_by(id: user_id)
          if user &amp;&amp; user.authenticated?(cookies:[:remember_token])
              log_in user
              @current_user = user
          end
      end
  end
  
  def logged_in?
      !current_user.nil?
  end
  
  def log_out
      debugger
      session.delete(:user_id)
      @current_user = nil
   end
  end
  </code></pre>
  
  <p>I use a DELETE request to '/logout'. </p>
  
  <pre><code> delete 'logout' =&gt; 'sessions#destroy'
  </code></pre>
  
  <p>The relevant part of the layout in the views </p>
  
  <pre><code>&lt;% if logged_in? %&gt;
      &lt;ul class="text-center nav navbar-nav navbar-right"&gt;
          &lt;li class="dropdown"&gt;
          &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"&gt;Account &lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;
          &lt;ul class="text-center dropdown-menu"&gt;
              &lt;li class="text-center"&gt;&lt;%= link_to "Profile", current_user %&gt;&lt;/li&gt;
              &lt;li class="text-center"&gt;&lt;%= link_to "Settings", "#" %&gt;&lt;/li&gt;
              &lt;li role="separator" class="divider"&gt;&lt;/li&gt;
              &lt;li class="text-center"&gt;&lt;%= link_to "Log Out", logout_path, :method =&gt; :delete %&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/li&gt;
      &lt;/ul&gt;
      &lt;% else %&gt;
      &lt;li&gt;&lt;a id="signin" href="/login"&gt;Sign In&lt;/a&gt;&lt;/li&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I believe the log_out method is not working because the view doesn't change, which according to the line <code>&lt;% if logged_in? %&gt;</code>, the logged_in method should prevent the part of view to be rendered when the session is deleted. Where 'logged_in?' method is defined in sessions_helper.rb that returns a boolean value true if the user is logged in.</p>
  
  <p>Entire routes.rb</p>
  
  <pre><code>Rails.application.routes.draw do
  
      root 'static_pages#home'
      get 'about' =&gt; 'static_pages#about'
      get 'help' =&gt; 'static_pages#help'
      get 'signup' =&gt; 'users#new'
      get  'login' =&gt; 'sessions#new'
      post  'login' =&gt; 'sessions#create'
      delete 'logout' =&gt; 'sessions#destroy'
      resources :users
  end
  </code></pre>
  
  <p>Entire sessions_controller.rb </p>
  
  <pre><code>class SessionsController &lt; ApplicationController
  
  def new
  end
  
  def create
      user = User.find_by(email: params[:session][:email].downcase)
      if user &amp;&amp; user.authenticate(params[:session][:password])
          log_in user
          remember user
          redirect_to user
      else
          flash.now[:danger] = 'Invalid email/password combination'
          render 'new'
      end
  end
  
  def destroy
      log_out
  end
  end
  </code></pre>
  
  <p>Entire sessions_helper.rb </p>
  
  <pre><code>module SessionsHelper
  def log_in(user)
      session[:user_id] = user.id
  end
  
  def remember(user)
    user.remember 
    cookies.permanent[:remember_token] = user.remember_token
    cookies.permanent.signed[:user_id] = user.id
  end
  
  def current_user
      if (user_id = session[:user_id])
          @current_user ||= User.find_by(id: session[:user_id])
      elsif (user_id = cookies.signed[:user_id])
          user = User.find_by(id: user_id)
          if user &amp;&amp; user.authenticated?(cookies:[:remember_token])
              log_in user
              @current_user = user
          end
      end
  end
  
  def logged_in?
      !current_user.nil?
  end
  
  def log_out
      session.delete(:user_id)
      @current_user = nil
  end
  end
  </code></pre>