Formtastic auswählen mit Gruppierung

Jetzt mit Formtastic habe ich einfach auswählen:

= f.input :category, :as => :select, :include_blank => false, :collection => subcategories

Hier zeige ich nur Kinderkategorien. ich benutzeacts_as_tree Plugin für Eltern-Kind-Beziehung. Ich möchte auch Elternkategorien anzeigen.

Formtastic generated select sollte wie folgt aussehen:

<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>

Verwendung der Gruppierung in Formtastic Select für Modelle mit Funktionen von acts_as_tree Weiss es jemand?

AKTUALISIERT

Ich fand heraus, dass dies funktionieren sollte:

= f.input :category, :include_blank => false, :group_by => :parent

aber nicht mit fehler:

undefined local variable or method `object_class' for #<Formtastic::SemanticFormBuilder:0x87d3158>

Es sieht so aus, als ob es einen Fehler in Formtastic gibt. Ich habe formtastic.rb durchgesehen und object_class in gefundenGruppenzuordnung erkennen Methode:

  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

Tatsächlichobject_class undefiniert in dieser Methode und es gibt keine private Methode mit diesem Namen in formtastic.rb. Aber wir können verwenden: group_association Assoziation explizit definieren.

- 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

aber ich bin auf einen anderen Fehler gestoßen:

undefined method `children' for nil:NilClass

Ich versuchte abzuschaltenActs_as_tree und schreibe meine eigenen selbstbezogenen Aussagen. Das Gleiche wie Acts_as_tree sollte aussehen:

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

Fehler ist das gleiche. Kann jemand helfen?

Aktualisiert

Nächster kleiner Schritt. Dieser Code ohne Formtastic funktioniert einwandfrei:

= grouped_collection_select('', :category_id, top_categories, :children, :name, :id, :name, :include_blank => true)

p.s: top_categories ist eine Hilfsmethode mit Auflistung der übergeordneten Kategorien.

Das Letzte ist, es in Formtastic-Syntax zu übersetzen :)

Antworten auf die Frage(3)

Ihre Antwort auf die Frage