未定义的方法logged_in? Ruby on Rails

时间:2016-08-09 17:24:50

标签: ruby-on-rails ruby devise cancan

我无法做到这一点(但我可能是个白痴)。我有一个类ApplicationController,它包含以下方法:

def logged_in?
  !current_user.nil?
end

if logged_in?
  load_and_authorize_resource :unless => :devise_controller?, :except => :show
end

这给了我一个错误:

  

未定义的方法`logged_in?' for ApplicationController:Class

这个方法是明确定义的,如何以未定义的方式返回?

2 个答案:

答案 0 :(得分:0)

也许宣布它的最佳位置是/helpers/application_helper.rb而不是application_controller。

此外,要验证用户是否已登录,Devise会提供以下帮助程序user_signed_in?

来源:https://github.com/plataformatec/devise

答案 1 :(得分:-1)

我认为您忘了写:

helper_method :logged_in?

ApplicationController中。