【问题标题】:Rails_admin, List of models show zero when using cancanRails_admin,使用 cancan 时模型列表显示为零
【发布时间】:2015-07-28 09:50:10
【问题描述】:

我有模型OwnerShopItem

Owner 有很多ShopsShops 有很多Items

我在康康舞方面的能力:

can :manage, Shop, owner_id: user.id
can :manage, Item, shop: {owner_id: user.id}

当我打开 rails_admin 仪表板时,它显示我的 Items 为零,而页面 List of Items 为空。

但是,当我打开商店页面时,我可以看到其中的所有商品,并且可以在商店页面上更改它们。

当我这样写代码时:

  can :manage, Item do |item|
    item.shop.owner_id == user.id
  end

它给我一个错误:

The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined for :index Item

为什么我不能在Items List 上列出我所有的Items

【问题讨论】:

    标签: ruby-on-rails cancan rails-admin cancancan


    【解决方案1】:

    我找到了一种解决方案。

    我需要向我的Item 模型添加附加字段:

    field :shop_owner_id, type: String
    

    那我需要赋值:

     before_save :set_shop_owner_id
    
     def set_shop_owner_id
       self.shop_owner_id = self.shop.owner.id.to_s
     end
    

    在我的ability.rb 文件中我需要添加这个:

    can :manage, Item, shop_owner_id: user.id
    

    这意味着,当rails_admin wnats 索引所有属于当前所有者的商店的商品时,出现了某种错误。这有助于避免它。

    我认为不是很好的解决方案,但有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-28
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 2022-01-12
      相关资源
      最近更新 更多