gpt4 book ai didi

ruby-on-rails - 如何使用 Active Admin(格式)按父项对复选框进行分组

转载 作者:行者123 更新时间:2023-12-03 18:34:20 24 4
gpt4 key购买 nike

这是类别模型。一个类别可以属于另一个类别。

class Category < ActiveRecord::Base
attr_accessible :title, :parent_id

has_and_belongs_to_many :products, :join_table => :products_categories

belongs_to :parent, :foreign_key => "parent_id", :class_name => "Category"
has_many :categories, :foreign_key => "parent_id", :class_name => "Category"
end

这是产品型号:
class Product < ActiveRecord::Base
attr_accessible :comment, location_id, :category_ids
has_and_belongs_to_many :categories, :join_table => :products_categories
belongs_to :location
end

在产品的 Active Admin 表单中,我想根据其 parent_id 对复选框进行分层排序,例如
  • 类别 1 [ ]
  • 类别 2 [ ]
  • 类别 3 [ ]
  • 类别 6 [ ]
  • 类别 4 [ ]
  • 类别 5 [ ]
  • 类别 7 [ ]

  • 以下是我对表格的了解:
    ActiveAdmin.register Product do
    form do |f|
    f.inputs "Product" do
    f.input :comment
    f.input :categories, :as => :check_boxes
    f.input :location
    end
    f.buttons
    end
    end

    目前,表单会拉入复选框并正确保存数据,但我不确定从哪里开始对它们进行分组。我查看了文档,但看不到任何明显的东西。

    最佳答案

    用户 Hopstream 的 ActiveAdmin -- How to display category taxonomy? (in tree type hierarchy) 可能会部分解决这个问题。题。它足够不同,因为 Formtastic 提出了一些有趣的挑战,但是,即 Formtastic 根本不能“开箱即用”地做到这一点。

    但是,可以扩展和覆盖 Formtastic 的 Formtastic::Inputs::CheckBoxesInput类以通过嵌套逻辑添加面条的能力。幸运的是,这个问题也已经发生在其他人身上。

    Github用户michelson的Formtastic check boxes with awesome_nested_set gist 将为您提供一个可以添加到 Rails 应用程序的类,放置 acts_as_nested_set线路在您的 Product模型和 f.input Formtastic 所需的行 f.inputs "Product"阻止您的 ActiveAdmin.register块,它实际上应该在不修改模型结构的情况下工作,如下所示:
    f.input :categories, :as=>:check_boxes, :collection=>Category.where(["parent_id is NULL"]) , :nested_set=>true

    关于ruby-on-rails - 如何使用 Active Admin(格式)按父项对复选框进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12971403/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com