如何使用Active Admin(formatastic)按父级对复选框进行分组

ben*_*nky 6 ruby-on-rails formtastic activeadmin

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

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
Run Code Online (Sandbox Code Playgroud)

这是产品型号:

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
Run Code Online (Sandbox Code Playgroud)

在产品的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
Run Code Online (Sandbox Code Playgroud)

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

小智 1

用户 Hopstream 的 ActiveAdmin 可以部分解决这个问题- 如何显示类别分类法?(在树类型层次结构中)问题。然而,由于 Formtastic 的存在,它的不同之处足以带来一些有趣的挑战,即直接使用 Formattastic 根本无法做到“开箱即用”。

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

Github 用户 michelson 的Formtastic 复选框与 Awesome_nested_set要点将为您提供一个可以添加到 Rails 应用程序中的类,将线放置acts_as_nested_set在模型中Product,并将f.inputFormtastic 块所需的线放置在块f.inputs "Product"ActiveAdmin.register,实际上,它应该在不修改结构的情况下工作您的模型为:

f.input :categories, :as=>:check_boxes, :collection=>Category.where(["parent_id is NULL"]) , :nested_set=>true