能力配置期间 CanCan 出现问题?
Issue With CanCan during abilities configuration?
我是 CanCan 的新手,正在 Rails 4.2.0 中使用它。我同时设置了用户和管理员权限,并且在尝试设置与 Visitor_parking 模型对话的用户权限时在 abilitiy.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 的管理所有功能,但是在管理部分中也会有规则,所以我想全部 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 业余爱好者,这是我第一次使用康康舞,所以它可能很小,我只是没有接受...任何帮助都非常重要赞赏!
您拼写 VisitorParking
错误:Visitor_parking
。尝试修复它。
老实说,我不确定它在你的条件 if
分支中如何工作。
我是 CanCan 的新手,正在 Rails 4.2.0 中使用它。我同时设置了用户和管理员权限,并且在尝试设置与 Visitor_parking 模型对话的用户权限时在 abilitiy.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 的管理所有功能,但是在管理部分中也会有规则,所以我想全部 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 业余爱好者,这是我第一次使用康康舞,所以它可能很小,我只是没有接受...任何帮助都非常重要赞赏!
您拼写 VisitorParking
错误:Visitor_parking
。尝试修复它。
老实说,我不确定它在你的条件 if
分支中如何工作。