title: Rails Devise current_user is undefined in a matches? advanced route constraint
body: <p>I get an error <code>NameError (undefined local variable or method "current_user" for #&lt;APIRouter:0x007ffd54a81f98&gt;):</code> when I try to use current_user in a <strong>matches?</strong> constraint.  I want certain dummy user to be routed to one set of controllers and other users to be routed to another set of controllers.  However, when I try to use current_user I get an error.  </p>
  
  <ul>
  <li>devise (2.0.4)</li>
  <li>rails (3.2.2)</li>
  </ul>
  
  <p>My matches constraint is defined in the APIRouter Class:</p>
  
  <pre><code>class APIRouter
    def matches? request
      @use_rm_app = ENV["USE_RM_APP"] == "true" || (current_user &amp;&amp; current_user.is_test)
    end
  end
  </code></pre>
  
  <p>Any ideas as to how I can use current user in the matches? constraint as defined in the <a href="http://guides.rubyonrails.org/routing.html#advanced-constraints" rel="noreferrer">Rails guide</a>.</p>
  
  <p>Section from the routes.rb file:</p>
  
  <pre><code>#  Route to the rm_app controller if APIRouter says we should, otherwise use the real backend controllers.
  match '/api/v1/*path', :controller =&gt; 'api/v1', 
    :action =&gt; 'handle_create_request', 
    :via =&gt; :post, :constraints =&gt; APIRouter.new
  </code></pre>
  
  <p><em><strong>UPDATE: Solved thanks to ksol.</em></strong>  Attempt 3 works.  Get user using <code>request.env["warden"].user(:user)</code>  </p>
  
  <p>Solution is to modify the APIRouter</p>
  
  <pre><code>  def matches? request
      user = request.env["warden"].user(:user)
      (user &amp;&amp; user.is_test) || ENV["USE_RM_APP"] == "true"
    end
  </code></pre>