我正在通过使用 selected 和 ancestry gem 来寻找动态选择,我希望以前有人这样做过。
我有大约 2000 个(类别、子类别和关键字)都在 STI 中并且具有树结构。在我的表单中,我为类别使用三个选择输入 | 子类别 | 关键字,但我得到了大量的子类别列表,甚至更大的关键字列表。我想隐藏所有不是预选类别的子类别的子类别;与关键字相同。
希望这是有道理的,我正在尝试做的事情。我将不胜感激任何想法。
到目前为止,这是我所拥有的,并且所有这些代码都运行良好。
class Company < ActiveRecord::Base
has_and_belongs_to_many :categories, :join_table => "companies_categories"
has_and_belongs_to_many :subcategories, :join_table => "companies_subcategories"
has_and_belongs_to_many :keywords, :join_table => "companies_keywords"
end
class Category < ActiveRecord::Base
has_ancestry :cache_depth => true, :depth_cache_column => :ancestry_depth
has_many :subcategories, :class_name => "Category", :foreign_key => :subcategory_id
has_many :keywords, :class_name => "Category", :foreign_key => :keyword_id
belongs_to :category
has_and_belongs_to_many :companies, :join_table => "companies_categories"
has_and_belongs_to_many :companies, :join_table => "companies_subcategories", :association_foreign_key => "subcategory_id"
has_and_belongs_to_many :companies, :join_table => "companies_keywords", :association_foreign_key => "keyword_id"
end
这是javascript
jQuery(function($){
$(".chosen-input").chosen();
$(".schosen-input").chosen();
$(".kchosen-input").chosen();
});
这是我的表格
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "New Company" do
f.input :name, :required => true
end
f.inputs "Categories" do
f.input :categories,
:input_html => { :class => "chosen-input", :multiple => true, :style => "width: 700px;"},
:collection => Category.where(:ancestry_depth => '2')
f.input :subcategories,
:input_html => { :class => "schosen-input", :multiple => true, :style => "width: 700px;"},
:collection => Category.where(:ancestry_depth => '3')
f.input :keywords,
:input_html => { :class => "schosen-input", :multiple => true, :style => "width: 700px;"},
:collection => Category.where(:ancestry_depth => [4, 5, 6])
end