【发布时间】:2019-12-25 19:42:33
【问题描述】:
我正在使用 Pundit,但找不到如何在我的策略中设置以问号结尾的方法。
我的会话控制器中有一个名为 is_logged_in 的方法?在会话策略中,我有相同的方法以及与之关联的正确安全逻辑。但是,当我尝试访问该方法时,我收到这些错误,因为该策略找不到该方法:
Minitest::UnexpectedError: NoMethodError: undefined method is_logged_in?? for #<SessionPolicy:0x007f9530200bd0>
当然,rails 方法不能有多个问号,那么我如何告诉我的策略这是方法?
我正在使用
- Ruby 2.3.3
- Rails 4.2.7.1
- 权威 2.1.0
政策:
class SessionPolicy < ApplicationPolicy
def create?
true
end
def new?
true
end
def destroy?
true
end
def is_logged_in?
true
end
end
用(Minitest)测试它:
test "is_logged_in? should return false when NOT logged in" do
sign_out get_user
get :is_logged_in?
assert_response :ok
response = response_to_json
assert !response[:logged_in]
assert !response[:is_business]
end
API:
def is_logged_in?
payload = {
logged_in: !current_user.nil?,
is_business: current_user.try(:is_business?)
}
render json: payload, status: :ok
end
除了这个单元测试和在使用 Pundit 进行授权之前,我能够从我的前端应用程序中达到这个端点。
【问题讨论】:
-
不幸的是我试过了,但没有用:(
标签: ruby-on-rails ruby pundit