我在 Controller 中有一些操作,并授权 3 个操作,
class TestController
def test1
authorize! :view, :testcase1
#do things1
end
def test2
authorize! :view, :testcase2
#do things2
end
def test3
authorize! :view, :testcase3
#do things3
end
end
在相应操作的 View 中,我正在这样检查
if can? :view, :test_case1
#do things
end
所以问题是我在 3 个函数中调用授权,我可以把它当作一个函数,与 before_filter 相同
最佳答案
我不太确定您的意思,但 load_andauthorize_resource
通常是最好的方法。如果您需要跳过 Controller 操作,可以执行以下操作:
class TestController
load_and_authorize_resource
skip_authorize_resource :only => :new
end
关于ruby-on-rails - 在 Rails 中使用 cancan 授权 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28971023/