我已经创建了一个应用程序,使用devise和cancan进行身份验证和授权。使用cancan,我定义了两个角色admin和operator。管理员可以管理所有,操作员可以编辑所有,但不能销毁,第三个是可以创建和管理的普通用户。但是代码只转到默认的else块。这是我的能力类和index.html
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :admin
can :manage, :all
elsif user.role? :operator
can :read, :all
else
can :read, :all
end
end
endindex.html
<h1>Listing todos</h1>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @todos.each do |todo| %>
<tr>
<td><%= todo.name %></td>
<td><%= todo.description %></td>
<% if can? :show, @todo %>
<td><%= link_to 'Show', todo %></td>
<% end %>
<% if can? :update, @todo %>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<% end %>
<% if can? :destroy, @todo %>
<td><%= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
</table>
<br />
<% if can? :destroy, @todo %>
<%= link_to 'New Todo', new_todo_path %>
<% end %>发布于 2012-04-19 16:24:47
根据您的即时设置,您的操作员权限和默认权限是相同的。他们只有权读取所有模型,而没有编辑它们的权限。
if user.role? :admin
can :manage, :all
elsif user.role? :operator
can :read, :all # no managing-abilities defined here
else
can :read, :all # same abilities as operator
end因此,如果您的role?-method工作正常,那么您的问题不是只有else--method被触发,而是操作员缺乏能力。
https://stackoverflow.com/questions/10221831
复制相似问题