【发布时间】:2016-12-11 07:59:15
【问题描述】:
使用 CanCanCan 授权和检查命名空间、无模型控制器的能力的正确方法是什么?
经过大量谷歌搜索和阅读 wiki,我目前拥有
#controllers/namespaces/unattacheds_controller.rb
def Namespaces::UnattachedsController
authorize_resource class: false
def create
# does some stuff
end
end
#models/ability.rb
def admin
can [:create], :namespaces_unattacheds
end
#view/
<%= if can? :create, :namespaces_unattacheds %>
# show a create form to authorized users
<% end %>
这未正确授权控制器。管理员可以看到条件创建表单,但无权发布到创建操作。
post :create, valid_params
Failure/Error: { it { expect( flash ).to have_content "Successfully created" }
expected to find text "Successfully created"
got: "You are not authorized to access this page."
在一个示例中,wiki 建议为命名空间控制器创建一个单独的 Ability 类。 https://github.com/CanCanCommunity/cancancan/wiki/Admin-Namespace
有没有更简单的方法来实现这一点?这个应用程序使用了许多命名空间的控制器,我真的不想为每个控制器创建一个能力类。
是否有正确的语法来引用 Ability 类中的命名空间控制器?
can [:create], Namespaces::Unattacheds
can [:create], :namespaces_unattacheds
can [:create], namespaces/unattacheds
????
【问题讨论】:
-
Namespaces::UnattachedsController是一个类,而不是一个定义,我认为。
标签: ruby-on-rails cancan cancancan