title: Redirect to Index if user is not logged in
body: <p>I have this in my application_controller.rb:</p>
  
  <pre><code>private
  
    def require_user_signed_in
      unless user_signed_in?
  
        # If the user came from a page, we can send them back.  Otherwise, send
        # them to the root path.
        if request.env['HTTP_REFERER']
          fallback_redirect = :back
        elsif defined?(root_path)
          fallback_redirect = root_path
        else
          fallback_redirect = "/"
        end
  
        redirect_to fallback_redirect, flash: {error: "You must be signed in to view this page."}
      end
    end
  </code></pre>
  
  <p>And I added this in my application.html.erb (the layout for the page) right after the start of the body:</p>
  
  <pre><code>&lt;% require_user_signed_in %&gt;
  </code></pre>
  
  <p><strong>My question is</strong> how why is it not working? I am getting an undefined local variable or method error. Am I not calling the method correctly? Do I have to define something somewhere for it to be usable in the layout erb?</p>
  
  <p>Thank you.</p>