Ruby 在能力配置过程中出现CanCan问题?

Ruby 在能力配置过程中出现CanCan问题?,ruby,ruby-on-rails-4,devise,cancan,Ruby,Ruby On Rails 4,Devise,Cancan,我是CanCan新手,我在Rails 4.2.0中使用它。我已经设置了用户和管理员权限,并且在尝试设置与访问者对话的用户权限时,在ability.rb中设置can和cannot语句时遇到了问题 我的ability.rb文件如下所示: class Ability include CanCan::Ability def initialize(user) if user.admin? #Grants Admin Permissions to Sites Model

我是CanCan新手,我在Rails 4.2.0中使用它。我已经设置了用户和管理员权限,并且在尝试设置与访问者对话的用户权限时,在ability.rb中设置can和cannot语句时遇到了问题

我的ability.rb文件如下所示:

class Ability
  include CanCan::Ability

  def initialize(user)

    if user.admin?
        #Grants Admin Permissions to Sites Model
        can :create, Site
        can :read, Site
        can :update, Site
        can :destroy, Site
        #Grants Admin Permissions to Residents Model
        can :create, Resident
        can :read, Resident
        can :update, Resident
        can :destroy, Resident
        #Grants Admin Permissions to Vehicles Model
        can :create, Vehicle
        can :read, Vehicle
        can :update, Vehicle
        can :destroy, Vehicle
        #Grants Admin Permissions to Parking Model
        can :create, Parking
        can :read, Parking
        can :update, Parking
        can :destroy, Parking
        #Grants Admin Permissions to Visitor Parking Model
        can :create, Visitor_parking
        can :read, Visitor_parking
        can :update, Visitor_parking
        can :destroy, Visitor_parking
        #Grants Admin Permissions to Trespass Model
        can :create, Trespass
        can :read, Trespass
        can :update, Trespass
        can :destroy, Trespass
        #Grants Admin Permissions to Reports Model
        can :create, Report
        can :read, Report
        can :update, Report do |report|
            report.user == user
        end
        cannot :destroy, Report
    else
        #Grants User Permissions to Sites Model
        cannot :create, Site
        can :read, Site
        cannot :update, Site
        cannot :destroy, Site
        #Grants User Permissions to Residents Model
        cannot :create, Resident
        can :read, Resident
        can :update, Resident
        cannot :destroy, Resident
        #Grants User Permissions to Vehicles Model
        cannot :create, Vehicle
        can :read, Vehicle
        can :update, Vehicle
        cannot :destroy, Vehicle
        #Grants User Permissions to Parking Model
        can :create, Parking
        can :read, Parking 
        can :update, Parking
        cannot :destroy, Parking
        #Grants User Permissions to Visitor Parking Model
        ##can :create, Visitor_parking
        ##can :read, Visitor_parking
        ##can :update, Visitor_parking
        ##cannot :destroy, Visitor_parking
        #Grants User Permissions to Trespass Model
        can :create, Trespass
        can :read, Trespass
        cannot :update, Trespass
        cannot :destroy, Trespass
        #Grants User Permissions to Reports Model
        can :create, Report
        can :read, Report
        can :update, Report do |report|
            report.user == user
        end
        cannot :destroy, Report

    end
我知道有些人会说,我应该使用can can的“管理所有”功能,但是在管理部分也会有规则,所以我想将其全部删除,并根据需要进行更改

问题在于上面代码的用户(else)部分。(用###注释掉的部分)管理员权限工作得很好,但是当我试图设置用户权限时,会出现以下错误,并且不确定该如何处理

我收到的错误消息:

Unable to autoload constant Visitor_parking, expected /Users/TaurenLTD1/Desktop/PatrolPro/Patrol/app/models/visitor_parking.rb to define it

    cannot :destroy, Parking
        #Grants User Permissions to Visitor Parking Model
      -->  can :create, Visitor_parking is what is hilighted red in the error message (but matches the admin perfectly)??? --->
        can :read, Visitor_parking
        can :update, Visitor_parking
        cannot :destroy, Visitor_parking
这就是VisitorParking模型的完整外观

class VisitorParking < ActiveRecord::Base

  # Adds Relationships to Visitor Parking
  belongs_to :user
  belongs_to :site

  # Adds Import / Export Functionality To Reports
   def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      VisitorParking.create! row.to_hash
    end
  end

  def self.to_csv
    CSV.generate do |csv|
      csv << column_names
      all.each do |visitor_parking|
        csv << visitor_parking.attributes.values_at(*column_names)
      end
    end
  end

end
class VisitorParkingcsv您拼写的
VisitorParking
错误:
Visitor\u parking
。试着把它修好


老实说,我不确定它在您的条件语句的
if
分支中如何工作。

首先,您使用的是哪个gem版本<代码>cancan
已经有一段时间没有维护了。您可能应该使用,由社区维护。@tompave,我目前正在使用'gem'cancan','~>1.6.10'您能分享社区更新verson的链接吗?我有。我之前的评论中链接了它。这个
不能:销毁,访客停车
不应该是
不能:销毁,访客停车
?您在这里重新引用了表名吗?我可以做一个cange表名迁移,我会让你知道这是否有效。不,模型名。您已将其声明为
VisitorParking
。型号名称为
Visitor parking
,而不是
Visitor\u parking
。请勿重命名此表。非常感谢!这就解决了问题!我正在安装cancancan,并将继续此操作。