title: Chapter 9, Exercise 6, Michael Hartl's Ruby on Rails Tutorial: What is the solution?
body: <p>In Michael Hartl's <em>Ruby on Rails Tutorial, 2nd Ed</em>, chapter 9's exercise 6, says:</p>
  
  <blockquote>
    <p>Signed-in users have no reason to access the new and create actions in
    the Users controller.  Arrange for such users to be redirected to the
    root URL if they do try to hit those pages.</p>
  </blockquote>
  
  <p>How does one write the rspec test for this? I tried this </p>
  
  <pre><code>  describe "POST on Users#create" do
      before { post users_path }
      specify { response.should redirect_to(root_path) }
    end
  </code></pre>
  
  <p>I have tried using a do/end block, adding a hash of user attributes, etc. The snippet above was added to line #162 in <a href="https://github.com/railstutorial/sample_app_2nd_ed/blob/master/spec/requests/authentication_pages_spec.rb" rel="nofollow">the official sample code</a>. All of them give me this error:</p>
  
  <pre><code>Failures:
  
    1) Authentication authorization as non-admin user POST on Users#create 
       Failure/Error: before { post users_path }
       AbstractController::DoubleRenderError:
         Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
       # ./app/controllers/users_controller.rb:27:in `create'
       # ./spec/requests/authentication_pages_spec.rb:72:in `block (5 levels) in &lt;top (required)&gt;'
  
  Finished in 2.72 seconds
  88 examples, 1 failure
  </code></pre>
  
  <p>As for the actual objective which is to limit access to the new and create actions in the Users controller, I solved them by adding this line:</p>
  
  <pre><code>admin_user if signed_in?
  </code></pre>
  
  <p>I know this works because I tested it by hand. The only problem is I am unable to write an rspec test for it. <a href="https://github.com/haloflightleader/sample_app" rel="nofollow">The code I have created as I follow this tutorial is available at github.</a></p>
  
  <p>Why am I getting this error? What am I doing wrong? What is the solution? Thank you.</p>