Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3.2 cancan中的检查能力_Ruby On Rails 3.2_Authorization_Cancan - Fatal编程技术网

Ruby on rails 3.2 cancan中的检查能力

Ruby on rails 3.2 cancan中的检查能力,ruby-on-rails-3.2,authorization,cancan,Ruby On Rails 3.2,Authorization,Cancan,我是一个相对的新手,我试图让cancan在一个简单的项目中工作。我已经阅读了Railscast并阅读了文档,但是我遗漏了一些东西 我有两个问题 我为普通用户正确隐藏了编辑/创建链接。但是,它们对管理员用户也是隐藏的 如果我注释掉隐藏上述链接的代码,当管理员用户单击链接时,他们会收到一条拒绝访问的消息,该消息应该显示给非管理员用户 所以我认为我的问题是应用程序没有正确地检查功能。我已经检查了数据库,并且该用户的“角色”列正确填充了“管理员” 以下是我的用户模型: class User < A

我是一个相对的新手,我试图让cancan在一个简单的项目中工作。我已经阅读了Railscast并阅读了文档,但是我遗漏了一些东西

我有两个问题

  • 我为普通用户正确隐藏了编辑/创建链接。但是,它们对管理员用户也是隐藏的
  • 如果我注释掉隐藏上述链接的代码,当管理员用户单击链接时,他们会收到一条拒绝访问的消息,该消息应该显示给非管理员用户
  • 所以我认为我的问题是应用程序没有正确地检查功能。我已经检查了数据库,并且该用户的“角色”列正确填充了“管理员”

    以下是我的用户模型:

    class User < ActiveRecord::Base
      has_secure_password
    
      #this is for authentication
      attr_accessible :email, :password, :password_confirmation, :role
      validates_uniqueness_of :email
      #end of authentication
    
     end
    
    根据角色隐藏控件的我的视图我有以下代码:

        <% if can? :manage, Instructor %>
    <td><%= link_to 'Edit', edit_instructor_path(instructor) %></td>
    <td><%= link_to 'Destroy', instructor, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <% end %>
    
    
    
    非常感谢您的帮助

    谢谢

    斯科特而不是这个

    <% if can? :manage, Instructor %>
    
    
    
    试试这个

    <% if user.has_role? :admin %>
    
    
    
    (假设在你的应用程序中,管理员管理/拥有比讲师更多的权限。)

    我更改了:

    if user.admin?
    

    它工作得很好

    if user.admin?
    
    if user.id