如何在不定义类能力的情况下在 cancan 中定义实例能力?
我想允许:manage
特定Course
实例的操作,而不是Course
class。
# ability.rb
can :manage, Course do |course|
# Check if the user is a helper for this course
CourseRole.get_role(user, course) == "helper"
end
这适用于实例变量:
# some_view.rb
can? :manage, @course # checks the instance to see if :manage is allowed
但如果我这样做:
# some_view.rb
can? :manage, Course
它总是返回 true,这很糟糕。
一些上下文:
class User < ActiveRecord::Base
has_many :course_roles
has_many :courses, :through => :course_roles
...
class CourseRoles < ActiveRecord::Base
belongs_to :user
belongs_to :course
...
class Courses < ActiveRecord::Base
has_many :course_roles
has_many :users, :through => :course_roles