在before filter
中未使用controller
我想检查user
是否经过身份验证,因为它会强制user
进行身份验证。
我正在寻找一种方法,如何在控制器中询问user
是否经过身份验证,并根据该决定从controller
发回的内容。
当前代码:
class Api::TestsController < Api::ApiController
before_filter :authenticate_user!
// the filter is force me to be an authenticated user to get access to this controller
end
我想要的是什么:
class Api::TestsController < Api::ApiController
def index
if current_user
// do something if the user is authenticated
else
// do something if the user not authenticated
end
end
end
我该如何实现? 我想要的是什么:
class Api::TestsController < Api::ApiController
def index
if current_user then
// do something if the user is authenticated
else
// do something if the user not authenticated
end
end
end
我该如何实现?
答案 0 :(得分:3)
那么,请使用方法user_signed_in?
。假设你的设计模型是User
。
class Api::TestsController < Api::ApiController
def index
if user_signed_in?
# do something if the user is authenticated
else
# do something if the user not authenticated
end
end
end