我正在使用devise
并试图强制用户登录。在他登录后,我想检查他的电子邮件是否在工作人员表中找到。如果存在,将他重定向到:/workers
,否则重定向到/tasksadmins
。
我试过:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!
before_filter :is_worker
def is_worker
@email = current_user.email
tag = Worker.where(:email => @email)
if tag.nil?
redirect_to '/tasksadmins'
else
redirect_to '/workers'
end
end
end
但我得到了:
undefined method `email' for nil:NilClass
更新
我试过:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!
before_filter :is_worker
def is_worker
if user_signed_in?
@email = current_user.try(:email)
if @email && Worker.find_by_email(@email).nil?
redirect_to '/tasksadmins'
else
redirect_to '/workers'
end
else
redirect_to '/users/sign_in' # devise?
end
end
end