title: Custom Devise 401 unauthorized response
body: <p>I'm working on a JSON-based API for my Rails 3.1 app.  I'd like to provide a custom failure response instead of the default, which is:</p>
  
  <pre><code>{"error":"You need to sign in or sign up before continuing."}
  </code></pre>
  
  <p>My API controller includes a before_filter call to <code>authenticate_user!</code>, which is what is rendering this JSON response.</p>
  
  <p>While searching, I came across <a href="https://stackoverflow.com/questions/4756922/devise-hijacking-a-401-response">this StackOverflow question</a>, which references <a href="https://github.com/plataformatec/devise/wiki/How-To:-Provide-a-custom-failure-response-with-Warden" rel="noreferrer">this Devise wiki entry</a>.  Unfortunately, the wiki entry isn't verbose enough for me to understand what it's telling me.  Specifically, I have no clue where I'm supposed to put that code such that Devise/Warden knows to render what I want returned.</p>
  
  <p>From the comments on the other SA question, it sounds like I don't need to call <code>custom_failure!</code> since I'm using a version of Devise above 1.2 (1.4.2 to be specific).  However, the wiki entry doesn't explain where the <code>render</code> call should go such that <code>authenticate_user!</code> knows to use that instead of its own render call.</p>
  
  <p>Where does this <code>render</code> call go?</p>
  
  <p><strong>Edit:</strong> I'm not just trying to change the message itself (a la the devise <code>en.yml</code> config); I'm trying to change the actual format of the response.  Specifically, I want to return this:</p>
  
  <pre><code>render :text =&gt; "You must be logged in to do that.", :status =&gt; :unauthorized
  </code></pre>