【问题标题】:Can not show multiple attributes (nested items) in Rails Active Admin Index page无法在 Rails 活动管理索引页面中显示多个属性(嵌套项)
【发布时间】:2018-09-01 06:17:38
【问题描述】:

我有两个模型 product 和 product_category 一个产品包含或映射有多个产品类别,反之亦然。 它是通过多对多关系维护的,其中有另一个模型称为 products_in_category。 我将 Active Admin 用于后端和 CRUD,现在我需要在 Active Admin 的产品索引页面中显示多个 product_categories。 任何建议或帮助都会对我很好。

#app/models/product.rb
class Product < ApplicationRecord
    has_many :products_in_categories
    has_many :product_categories, through: :products_in_categories, dependent: :destroy
    accepts_nested_attributes_for :product_categories   
end

#app/models/product_category.rb
class ProductCategory < ApplicationRecord
    has_many :products_in_categories
    has_many :products, through: :products_in_categories, dependent: :destroy
    accepts_nested_attributes_for :products
    accepts_nested_attributes_for :products_in_categories
end

#app/models/products_in_category.rb
class ProductsInCategory < ApplicationRecord
    belongs_to :product_category
    belongs_to :product
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 activeadmin ruby-on-rails-5.1


    【解决方案1】:

    您可以在一列中列出类别名称(只需将name 更改为您的实际属性):

    index do
      # other columns goes here
      column('Categories') { |p| p. product_categories.pluck(:name).join(', ') }
    end
    

    【讨论】:

      • 而不是 (', ') 列出所有这些怎么样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 2015-11-25
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多