title: rails 4.0 view current user if statement not working correctly
body: <p>I have an if statement in my view which displays a log in form if the user is not logged in or a logout button if they are.  I am new to ruby and to rails so I dont know how to troubleshoot this properly.</p>
  
  <p>Currently if I log in with the form I get the logged in flash message but the log in form remains.  If I create a new user it logs me in automatically upon creation and the log out button then displays in place of the log in form however when I select log out it logs me out, returns me to the home page and flashes logged out but the logout button remains instead of being replaced again by the log in form</p>
  
  <p>application.html.erb</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;link rel="stylesheet" href="/css/style.css"&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;header&gt;
          &lt;div class="wrapper"&gt;
              &lt;img src="/gfx/logo.png"&gt;
              &lt;span&gt;Twitter Clone&lt;/span&gt;
  
              &lt;% if current_user %&gt;
                  &lt;%= link_to "Log Out", sessions_destroy_path %&gt;
              &lt;% else %&gt;
                  &lt;%= form_tag sessions_create_path do %&gt;
                      &lt;%= text_field_tag :username, nil, placeholder: "username" %&gt;
                      &lt;%= password_field_tag :password, nil,  placeholder: "password" %&gt;
                      &lt;%= submit_tag "Log In" %&gt;
                  &lt;% end %&gt;
              &lt;% end %&gt;
  
          &lt;/div&gt;
      &lt;/header&gt;
      &lt;div id="content"&gt;
          &lt;div class="wrapper"&gt;
              &lt;% flash.each do |name, msg| %&gt;
                  &lt;%= content_tag :div, msg, class: "flash #{name}" %&gt;
              &lt;% end %&gt;
              &lt;%= yield %&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;footer&gt;
  
         &lt;div class="wrapper"&gt;
              Ribbit - A Twitter Clone in Ruby&lt;img src="/gfx/logo-nettuts.png"&gt;
          &lt;/div&gt;
      &lt;/footer&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>application_controller.rb</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception
  
    private
  
    def current_user
      @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  
  helper_method :current_user
  end
  </code></pre>
  
  <p>sessions_controller.rb</p>
  
  <pre><code>class SessionsController &lt; ApplicationController
    def new
    end
  
    def create
      user = User.find_by_username(params[:username])
          if user &amp;&amp; user.authenticate(params[:password])
              session[:userid] = user.id
              redirect_to root_url, notice: "Logged in!"
          else
              flash[:error] = "Wrong Username or Password."
              redirect_to root_url
          end
    end
  
    def destroy
      session[:userid] = nil
      redirect_to root_url, notice: "Logged out."
    end
  end
  </code></pre>