铁轨 +康康 +多态关联未按预期工作
我正在尝试让 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
新的并创建操作对于 user_areas 和 account_areas 都很有效,但是当我尝试转到区域的索引操作时,我收到以下错误:
NameError in AreasController#index
uninitialized constant Owner
有什么想法吗?非常感谢
I am trying to get cancan to work with a polymorphic association, i have read through the docs and wiki and am not able to get it working...
I have the following models:
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
and in the controller:
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
and the following abilities:
can :manage, Area, :owner => { :memberships => { :user => { :id => user.id } } } # Accounts through Membership
can :manage, Area, :owner => { :id => user.id } # User
the new and create actions work great for both user_areas and account_areas but when i try to go to the index action of areas i get the following error:
NameError in AreasController#index
uninitialized constant Owner
Any thoughts? many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它认为您的类名为
Owner
,也许您可以尝试:如果不起作用,请查看此问题是否解决您的问题:https://github.com/ryanb/cancan/issues/73
根据票证,如果您使用的是 1.3 或更高版本,那么您可以执行以下操作:
但是在这种情况下你的
authorize_parent
方法更改如下:有关详细文档,请参阅标题
多态关联
:https://github.com/ryanb/cancan/wiki/Nested-Resources让我知道是否适合您。干杯安迪;)
It considers that your class is called
Owner
, maybe you can try like:If dont works, see if this issue solves your problem: https://github.com/ryanb/cancan/issues/73
Based on the ticket if you are using 1.3 or later version then you can do the following:
But in this case your
authorize_parent
method changes like this:For detailed documentation please see the title
Polymorphic associations
on : https://github.com/ryanb/cancan/wiki/Nested-ResourcesLet me know if works for you. Cheers Andy ;)