Note that there are some explanatory texts on larger screens.

plurals
  1. POFormtastic select with grouping
    primarykey
    data
    text
    <p>Now with Formtastic I have plain select:</p> <pre><code>= f.input :category, :as =&gt; :select, :include_blank =&gt; false, :collection =&gt; subcategories </code></pre> <p>Here I show only children categories. I use <strong>acts_as_tree</strong> plugin for parent-child relationship. I would like to show parent categories as well.</p> <p>Formtastic generated select should look like this one:</p> <pre><code>&lt;select name="favoritefood"&gt; &lt;optgroup label="Dairy products"&gt; &lt;option&gt;Cheese&lt;/option&gt; &lt;option&gt;Egg&lt;/option&gt; &lt;/optgroup&gt; &lt;optgroup label="Vegetables"&gt; &lt;option&gt;Cabbage&lt;/option&gt; &lt;option&gt;Lettuce&lt;/option&gt; &lt;option&gt;Beans&lt;/option&gt; &lt;option&gt;Onions&lt;/option&gt; &lt;option&gt;Courgettes&lt;/option&gt; &lt;/optgroup&gt; ⋮ &lt;/select&gt; </code></pre> <p>How to use grouping in Formtastic select for model with acts_as_tree functionality? Does anybody know? </p> <p><strong>UPDATED</strong></p> <p>I figured out that this should work:</p> <pre><code>= f.input :category, :include_blank =&gt; false, :group_by =&gt; :parent </code></pre> <p>but it doesn't with error:</p> <pre><code>undefined local variable or method `object_class' for #&lt;Formtastic::SemanticFormBuilder:0x87d3158&gt; </code></pre> <p>It looks like there is some bug in Formtastic. I have looked through formtastic.rb and found object_class in <strong>detect_group_association</strong> method:</p> <pre><code> 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 =&gt; :customer # eg. # class Project # belongs_to :parent, :class_name =&gt; 'Project', :foreign_key =&gt; '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 =&gt; 'Project', :foreign_key =&gt; 'parent_id' # belongs_to :customer # end # class Customer # has_many :tasks, :class_name =&gt; 'Project', :foreign_key =&gt; '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 </code></pre> <p>Indeed <strong>object_class</strong> undefined in this method and there is no privat method with that name in formtastic.rb. But we can use <strong>:group_association</strong> to define association explicitly.</p> <pre><code>- semantic_form_for ([:manager, @purchase_profile]) do |f| - f.inputs do = f.input :category, :include_blank =&gt; false, :group_by =&gt; :parent, :group_association =&gt; :children = f.buttons </code></pre> <p>but I ran into another error:</p> <pre><code>undefined method `children' for nil:NilClass </code></pre> <p>I tried swith off <strong>Acts_as_tree</strong> and write my own self-referenced assositions. The same as Acts_as_tree works should look like:</p> <pre><code>class Category &lt; ActiveRecord::Base belongs_to :parent, :class_name =&gt; "Category", :foreign_key =&gt; "parent_id" has_many :children, :class_name =&gt; "Category", :foreign_key =&gt; "parent_id" end </code></pre> <p>Error is the same. Can anybody help?</p> <p><strong>Updated</strong></p> <p>Next little step. This code without Formtastic works fine:</p> <pre><code>= grouped_collection_select('', :category_id, top_categories, :children, :name, :id, :name, :include_blank =&gt; true) </code></pre> <p>p.s: top_categories is helper method with collection of parent categories.</p> <p>The last thing is translate it into Formtastic syntax :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload