【问题标题】:Devise current_user helper method only works in users_controller.rb设计 current_user 辅助方法仅适用于 users_controller.rb
【发布时间】:2017-08-09 12:34:28
【问题描述】:

正如标题所示,设计辅助方法仅在 users_controller.rb 中有效。如果我在登录后尝试在任何其他控制器(甚至是 applications_controller.rb)中访问它,我会收到此错误。

Routing Error
undefined local variable or method `current_user' for ApplicationController:Class

我只是在 applications_controller.rb 中添加 puts current_user

这是我的routes.rb

 devise_for :users
 resources :users

知道为什么会这样吗?谢谢。

【问题讨论】:

标签: ruby-on-rails devise


【解决方案1】:

current_user 不是类方法,因此不能直接在 ApplicationController 中使用

它将在所有actionbefore_action 中可用

使用以下逻辑检查应用程序控制器中current_user 的值。

class ApplicationController < ActionController::Base
  before_action do
    puts current_user
  end
  protect_from_forgery with: :exception
end

如果您仍然遇到任何问题,请告诉我

【讨论】:

  • 它出现了。我收到错误是因为“puts current_user”不在方法/函数中。
  • 很高兴为您提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 2023-04-08
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多