首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails : cancan问题

Rails : cancan问题
EN

Stack Overflow用户
提问于 2012-04-19 12:52:32
回答 1查看 84关注 0票数 0

我已经创建了一个应用程序,使用devise和cancan进行身份验证和授权。使用cancan,我定义了两个角色admin和operator。管理员可以管理所有,操作员可以编辑所有,但不能销毁,第三个是可以创建和管理的普通用户。但是代码只转到默认的else块。这是我的能力类和index.html

代码语言:javascript
复制
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
end

index.html

代码语言:javascript
复制
  <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 %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-19 16:24:47

根据您的即时设置,您的操作员权限和默认权限是相同的。他们只有权读取所有模型,而没有编辑它们的权限。

代码语言:javascript
复制
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被触发,而是操作员缺乏能力。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10221831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档