2012-10-19 9 views
5

這是分類模型。一個類別可以屬於另一個類別。如何通過Active Admin對父項進行分組複選框(formatastic)

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 

在活動管理員形成了對產品我要訂購層次根據他們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 

目前形式的複選框拉和正確保存數據,但我不知道從哪裏開始分組。我瀏覽了文檔,但看不到任何明顯的內容。

回答

1

這可能部分地由用戶Hopstream的ActiveAdmin -- How to display category taxonomy? (in tree type hierarchy)問題產生。由於Formtastic提出了一些有趣的挑戰,這是不同的,但是,formtastic直接不能做到「開箱即用」。

然而,擴展並覆蓋Formtastic的Formtastic::Inputs::CheckBoxesInput類是可能的,以便通過嵌套邏輯添加麪條的功能。幸運的是,這個問題也已經發生在別人身上。

Github的用戶邁克爾遜Formtastic check boxes with awesome_nested_set要點將爲您提供一個類,你可以添加到您的Rails應用程序,將在您的Product模型acts_as_nested_set線和必要的Formtastic f.inputs "Product"f.input線的ActiveAdmin.register塊內,這實際上應該工作從模型的結構不變:

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

+0

這似乎對我來說,這絕不是一個頻繁出現的問題。在某些測試中,有些人可能會通過https://github.com/justinfrench/formtastic獲得pull request,並在實際的Formtastic :: Inputs :: CheckBoxesInput基類中進行這些更改。如果我有時間的話,我可能會這樣,但是在我能找到一些空閒之前,這可能不會持續幾個星期。 – jimcavoli

相關問題