【发布时间】:2015-08-03 17:42:08
【问题描述】:
我是 CanCan 的新手,正在 Rails 4.2.0 中使用它。我设置了用户和管理员权限,并且在尝试设置与 Visitor_parking 模型对话的用户权限时在 abilitiy.rb 中设置 can 和 cannot 语句时遇到了问题。
我的能力.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 的管理所有功能,但是在管理部分也会有规则,所以我想把它全部 CRUD 并根据需要进行更改。
问题在于上述代码的用户(其他)部分。 (用 ## 注释掉的部分)管理员权限可以正常工作,但是当我尝试设置用户权限时,我收到以下错误并且不知道该去哪里。
我得到的错误信息:
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
我有点像 Rails 爱好者,这是我第一次使用 cancan,所以这可能是一些小事,我只是没有接受......任何帮助都非常感谢!
【问题讨论】:
-
首先,您使用的是哪个 gem 版本?
cancan已经有一段时间没有维护了。您可能应该使用由社区维护的cancancan。 -
@tompave ,我目前正在使用 'gem 'cancan', '~> 1.6.10' 你能分享社区更新版本的链接吗?
-
我做到了。它链接在我之前的评论中。
-
这个
cannot :destroy, Visitor_parking不应该是cannot :destroy, VisitorParking吗?
标签: ruby ruby-on-rails-4 devise cancan