1

如何將cancan,inherited_resources和單個表繼承一起使用? 我有類似的代碼這個例子:CanCan,InheritedResources和STI

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 

當我嘗試爲用戶創建人,我得慘慘::存取遭拒。這是因爲InheritedResources使用Contact作爲:resource_class。

回答

2

我發現這個解決方案:

class ContactsController < InheritedResources::Base 
    alias :resource_class :contact_sti 
end