我在控制器中有一些操作,并将authorize设置为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在相应操作的视图中,我像这样检查
if can? :view, :test_case1
#do things
end所以问题是我在3个函数中调用authorize,我可以把它说成是一个单一的,和before_filter一样的吗?
发布于 2015-03-11 04:46:45
我不太确定你在说什么,但是load_and authorize_resource通常是最好的选择。如果你需要跳过一个控制器动作,你可以这样做:
class TestController
load_and_authorize_resource
skip_authorize_resource :only => :new
end发布于 2015-03-11 02:21:25
将此代码添加到类中
class TestController
load_and_authorize_resource
end并且不需要在每个操作中都使用authorize
https://github.com/ryanb/cancan/wiki/authorizing-controller-actions
https://stackoverflow.com/questions/28971023
复制相似问题