Ruby on rails 我如何在cancan中定义实例能力而不定义类能力?
我如何在cancan中定义实例能力而不定义类能力 我想允许Ruby on rails 我如何在cancan中定义实例能力而不定义类能力?,ruby-on-rails,ruby,cancan,Ruby On Rails,Ruby,Cancan,我如何在cancan中定义实例能力而不定义类能力 我想允许:管理特定课程实例的操作,而不是课程类 # 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 # ch
:管理特定课程
实例的操作,而不是课程
类
# 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
class用户:课程角色
...
类CourseRoles:course\u角色
而不是can?:管理,当然,您可以使用can?:管理课程。新建
,并确保新课程对象未通过您在ability.rb中传递的模块