【发布时间】:2012-01-30 21:56:29
【问题描述】:
我正在尝试让 cancan 与多态关联一起工作,我已通读文档和 wiki,但无法使其正常工作...
我有以下型号:
class User < ActiveRecord::Base
has_many :areas, :as => :owner, :dependent => :destroy
end
class Account < ActiveRecord::Base
has_many :areas, :as => :owner, :dependent => :destroy
end
class Area < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
end
在控制器中:
class AreasController < ApplicationController
load_resource :user, :instance_name => :owner
load_resource :account, :instance_name => :owner
load_and_authorize_resource :area, :through => :owner
before_filter :authorize_parent
respond_to :html
def authorize_parent
authorize! :manage, @owner
end
def index
end
def show
@events = @area.events.page(params[:page]).per(5)
respond_with @area
end
def new
respond_with @area
end
def create
@area = @owner.areas.new(params[:area])
if @area.save
flash[:notice] = "Your new area has been created..."
end
respond_with @area
end
end
以及以下能力:
can :manage, Area, :owner => { :memberships => { :user => { :id => user.id } } } # Accounts through Membership
can :manage, Area, :owner => { :id => user.id } # User
new 和 create 操作对 user_areas 和 account_areas 都很有效,但是当我尝试转到区域的索引操作时,我收到以下错误:
NameError in AreasController#index
uninitialized constant Owner
有什么想法吗?非常感谢
【问题讨论】:
-
你能粘贴你的控制器代码吗?
-
抱歉有点疏忽...
标签: ruby-on-rails-3.1 cancan polymorphic-associations