6

This is the category model. A category can belong to another category.

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

This is the product model:

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

In the Active Admin form for a product I want to hierarchically order the checkboxes based on their parent_id e.g.

  • Category 1 [ ]
    • Category 2 [ ]
    • Category 3 [ ]
  • Category 6 [ ]
    • Category 4 [ ]
  • Category 5 [ ]
  • Category 7 [ ]

Below is as far as I've got with the form:

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

Currently the form pulls in the checkboxes and saves the data correctly but I'm not sure where to begin with grouping them. I looked through the documentation but couldn't see anything obvious.

4

1 回答 1

1

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

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

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

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

于 2013-04-02T13:40:49.630 回答