铁轨 +康康 +多态关联未按预期工作

发布于 2025-01-01 00:45:29 字数 1609 浏览 0 评论 0原文

我正在尝试让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2025-01-08 00:45:29

它认为您的类名为 Owner,也许您可​​以尝试:

load_resource :user, :instance_name => :owner, :class=>'User'
load_resource :account, :instance_name => :owner, :class=>'Account'
load_and_authorize_resource :area, :through => :owner

如果不起作用,请查看此问题是否解决您的问题:https://github.com/ryanb/cancan/issues/73

根据票证,如果您使用的是 1.3 或更高版本,那么您可以执行以下操作:

load_resource :user
load_resource :account
load_and_authorize_resource :area, :through => [:user, :account]

但是在这种情况下你的authorize_parent 方法更改如下:

def authorize_parent
  authorize! :manage, (@user||@account)
end    

有关详细文档,请参阅标题多态关联https://github.com/ryanb/cancan/wiki/Nested-Resources

让我知道是否适合您。干杯安迪;)

It considers that your class is called Owner, maybe you can try like:

load_resource :user, :instance_name => :owner, :class=>'User'
load_resource :account, :instance_name => :owner, :class=>'Account'
load_and_authorize_resource :area, :through => :owner

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:

load_resource :user
load_resource :account
load_and_authorize_resource :area, :through => [:user, :account]

But in this case your authorize_parent method changes like this:

def authorize_parent
  authorize! :manage, (@user||@account)
end    

For detailed documentation please see the title Polymorphic associations on : https://github.com/ryanb/cancan/wiki/Nested-Resources

Let me know if works for you. Cheers Andy ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文