现在选择我有平原选择:Formtastic与Formtastic分组
= f.input :category, :as => :select, :include_blank => false, :collection => subcategories
在这里,我只显示儿童的类别。我使用acts_as_tree插件为亲子关系。我也想显示父类别。
Formtastic产生的选择应该是这样的一个:
<select name="favoritefood">
<optgroup label="Dairy products">
<option>Cheese</option>
<option>Egg</option>
</optgroup>
<optgroup label="Vegetables">
<option>Cabbage</option>
<option>Lettuce</option>
<option>Beans</option>
<option>Onions</option>
<option>Courgettes</option>
</optgroup>
⋮
</select>
如何使用分组在Formtastic选择模型acts_as_tree功能?有人知道吗?
修订
我想通了,这应该工作:
= f.input :category, :include_blank => false, :group_by => :parent
,但它不会出现错误:
undefined local variable or method `object_class' for #<Formtastic::SemanticFormBuilder:0x87d3158>
它看起来像有在Formtastic一些bug 。我已经通过formtastic.rb看去,在detect_group_association方法发现object_class:
def detect_group_association(method, group_by)
object_to_method_reflection = self.reflection_for(method)
method_class = object_to_method_reflection.klass
method_to_group_association = method_class.reflect_on_association(group_by)
group_class = method_to_group_association.klass
# This will return in the normal case
return method.to_s.pluralize.to_sym if group_class.reflect_on_association(method.to_s.pluralize)
# This is for belongs_to associations named differently than their class
# form.input :parent, :group_by => :customer
# eg.
# class Project
# belongs_to :parent, :class_name => 'Project', :foreign_key => 'parent_id'
# belongs_to :customer
# end
# class Customer
# has_many :projects
# end
group_method = method_class.to_s.underscore.pluralize.to_sym
return group_method if group_class.reflect_on_association(group_method) # :projects
# This is for has_many associations named differently than their class
# eg.
# class Project
# belongs_to :parent, :class_name => 'Project', :foreign_key => 'parent_id'
# belongs_to :customer
# end
# class Customer
# has_many :tasks, :class_name => 'Project', :foreign_key => 'customer_id'
# end
possible_associations = group_class.reflect_on_all_associations(:has_many).find_all{|assoc| assoc.klass == object_class}
return possible_associations.first.name.to_sym if possible_associations.count == 1
raise "Cannot infer group association for #{method} grouped by #{group_by}, there were #{possible_associations.empty? ? 'no' : possible_associations.size} possible associations. Please specify using :group_association"
end
事实上object_class不确定这个方法并没有在formtastic.rb该名称没有普里瓦方法。但是我们可以使用:group_association来明确定义关联。
- semantic_form_for ([:manager, @purchase_profile]) do |f|
- f.inputs do
= f.input :category, :include_blank => false, :group_by => :parent, :group_association => :children
= f.buttons
,但我遇到了另一个错误:
undefined method `children' for nil:NilClass
我试着开关等关Acts_as_tree和写我自己的自我引用assositions。与Acts_as_tree相同的作品应该如下所示:
class Category < ActiveRecord::Base
belongs_to :parent, :class_name => "Category", :foreign_key => "parent_id"
has_many :children, :class_name => "Category", :foreign_key => "parent_id"
end
错误是一样的。任何人都可以帮忙吗?
更新
下一个小小的一步。此代码没有Formtastic工作正常:
= grouped_collection_select('', :category_id, top_categories, :children, :name, :id, :name, :include_blank => true)
p.s:top_categories是辅助方法与父类别的集合。
的最后一件事就是把它翻译成Formtastic语法:)
你有任何运气得到这个在Formtastic中正常工作? – Codebeef 2010-03-10 10:59:49
还没有。我在github向Formtastic的人提交了一个问题。没有关于分组选项的规范覆盖范围,并且将不胜感激。 – Voldy 2010-03-10 13:42:00
你知道了吗? – 2011-10-18 12:51:30