如何一起使用cancan,inherited_resources和单表继承? 我有类似这个例子的代码:
class Contact < ActiveRecord::Base; end
class Person < Contact; end
class Company < Contact; end
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # in case of guest
can :read, Contact # User can read People and Companies
can :create, Person # User can create Person only
can :manage, :all if user.has_role? :admin
end
end
class ContactsController < InheritedResources::Base
load_and_authorize_resource
def new
@contact = contact_sti.new
end
private
def clazz
self.params[:contact_type].nil? ? "contact" : self.params[:contact_type]
end
def contact_sti
clazz.camelize.constantize
end
end
当我尝试以用户身份创建Person时,我获得了CanCan :: AccessDenied。那是因为InheritedResources使用Contact as:resource_class。
答案 0 :(得分:2)
我找到了这个解决方案:
class ContactsController < InheritedResources::Base
alias :resource_class :contact_sti
end