NameError (undefined local variable or method "current_user" for #<APIRouter:0x007ffd54a81f98>):
マッチで current_user を使用しようとするとエラーが発生しますか? 制約。特定のダミー ユーザーを 1 つのコントローラー セットにルーティングし、他のユーザーを別のコントローラー セットにルーティングしたいと考えています。ただし、 current_user を使用しようとすると、エラーが発生します。
- 考案 (2.0.4)
- レール (3.2.2)
私の一致制約は APIRouter クラスで定義されています。
class APIRouter
def matches? request
@use_rm_app = ENV["USE_RM_APP"] == "true" || (current_user && current_user.is_test)
end
end
試合で現在のユーザーをどのように使用できるかについてのアイデアはありますか? Rails ガイドで定義されている制約。
routes.rb ファイルのセクション:
# 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