Ahora con Formtastic tengo Normal Selección:Formtastic seleccione la agrupación
= f.input :category, :as => :select, :include_blank => false, :collection => subcategories
Aquí muestro niños solamente categorías. Yo uso act_as_tree plugin para la relación padre-hijo. Me gustaría mostrar categorías de padres también.
Formtastic genera seleccione debe ser similar a éste:
<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>
Cómo utilizar agrupación en Formtastic seleccione para el modelo con acts_as_tree funcionalidad? ¿Alguien sabe?
ACTUALIZADO
me di cuenta de que esto debería funcionar:
= f.input :category, :include_blank => false, :group_by => :parent
pero no lo hace con el error:
undefined local variable or method `object_class' for #<Formtastic::SemanticFormBuilder:0x87d3158>
Parece que hay algunos errores en Formtastic . He mirado a través formtastic.rb y se encontró object_class en detect_group_association método:
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
De hecho object_class no definido en este método y no existe un método privado con ese nombre en formtastic.rb. Pero podemos usar : group_association para definir la asociación explícitamente.
- 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
pero me encontré con otro error:
undefined method `children' for nil:NilClass
me trataron swith fuera Acts_as_tree y escribir mi propio assositions auto-referenciado. Igual que los trabajos de Acts_as_tree deberían verse así:
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
El error es el mismo. ¿Alguien puede ayudar?
Actualizado
pequeño paso siguiente. Este código sin Formtastic funciona bien:
= grouped_collection_select('', :category_id, top_categories, :children, :name, :id, :name, :include_blank => true)
p.s: top_categories es método de ayuda con la colección de categorías padre.
Lo último es traducirlo a la sintaxis Formtastic :)
¿Ha tenido un poco de suerte con conseguir esto funciona correctamente en Formtastic? – Codebeef
Aún no. He enviado un problema a los chicos de Formtastic en github. No hay cobertura de especificaciones para las opciones agrupadas y se apreciará una especificación anómala. – Voldy
¿Has descubierto esto? –