Rails设计检查验证用户没有过滤器

时间:2015-08-23 18:49:32

标签: ruby-on-rails filter devise

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

我该如何实现?

1 个答案:

答案 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