Rails devise current_user undefined in matches? extended route restriction

I get an error NameError (undefined local variable or method "current_user" for #<APIRouter:0x007ffd54a81f98>):when I try to use current_user to match the restriction . I want some dummy user to be redirected to one set of controllers and other users to route to another set of controllers. However, when I try to use current_user, I get an error.

  • devise (2.0.4)
  • rails (3.2.2)

The restriction of my matches is defined in the APIRouter class:

class APIRouter
  def matches? request
    @use_rm_app = ENV["USE_RM_APP"] == "true" || (current_user && current_user.is_test)
  end
end

Any ideas on how I can use the current user in matches? restriction defined in the Rails guide .

Section from routes.rb file:

#  Route to the rm_app controller if APIRouter says we should, otherwise use the real backend controllers.
match '/api/v1/*path', :controller => 'api/v1', 
  :action => 'handle_create_request', 
  :via => :post, :constraints => APIRouter.new

: ksol. 3 . request.env["warden"].user(:user)

, APIRouter

  def matches? request
    user = request.env["warden"].user(:user)
    (user && user.is_test) || ENV["USE_RM_APP"] == "true"
  end
+5
3

current_user . , , .

if current_user, , , constraints: APIRouter.new(current_user). APIRouter#initialize current_user var, matches?.

2

, :

authenticated :user do
  root :to => "main#dashboard"
end

, user.is_test .

3

. request.env["warden"].user(:user) , .

+10

ksol user.is_test, :

authenticated :user, lambda {|u| u.is_test } do
    // route stuff here
end

, u.is_test .

+6

Do not forget to add diff: for both root, and through an error

authenticated :user do
  root :to => "main#dashboard", :as => :foo
end

root :to => "main#landing_page"
0
source

All Articles