Formtastic выберите с группировкой
Теперь с Formtastic у меня есть простой выбор:
= f.input :category, :as => :select, :include_blank => false, :collection => subcategories
Здесь я показываю только детские категории. я используюacts_as_tree Плагин для родительско-дочерних отношений. Я хотел бы также показать родительские категории.
Сгенерированный Formtastic select должен выглядеть так:
Cheese
Egg
Cabbage
Lettuce
Beans
Onions
Courgettes
⋮
Как использовать группировку в Formtastic select для модели с функционалом acts_as_tree? Кто-нибудь знает?
ОБНОВЛЕНО
Я понял, что это должно работать:
= f.input :category, :include_blank => false, :group_by => :parent
но это нет с ошибкой:
undefined local variable or method `object_class' for #
Похоже, в Formtastic есть какая-то ошибка. Я просмотрел formtastic.rb и нашел object_class вdetect_group_association метод:
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 и написать мои собственные предположения. Так же, как работает 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 :)