Formtastic select with grouping

2020-07-07 11:19发布

Now with Formtastic I have plain select:

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

Here I show only children categories. I use acts_as_tree plugin for parent-child relationship. I would like to show parent categories as well.

Formtastic generated select should look like this one:

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

How to use grouping in Formtastic select for model with acts_as_tree functionality? Does anybody know?

UPDATED

I figured out that this should work:

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

but it doesn't with error:

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

It looks like there is some bug in Formtastic. I have looked through formtastic.rb and found object_class in detect_group_association method:

  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

Indeed object_class undefined in this method and there is no privat method with that name in formtastic.rb. But we can use :group_association to define association explicitly.

- 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

but I ran into another error:

undefined method `children' for nil:NilClass

I tried swith off Acts_as_tree and write my own self-referenced assositions. The same as Acts_as_tree works should look like:

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

Error is the same. Can anybody help?

Updated

Next little step. This code without Formtastic works fine:

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

p.s: top_categories is helper method with collection of parent categories.

The last thing is translate it into Formtastic syntax :)

3条回答
甜甜的少女心
2楼-- · 2020-07-07 11:26

Why not try building your list of items in a helper and then passing that into the select tag?

I don't know much about formtastic, or acts_as_tree, but if you are having problems getting it working how you have above, I think resorting to building your options before passing them to your select is perfectly reasonable.

查看更多
在下西门庆
3楼-- · 2020-07-07 11:30

In case anyone is having the same problem, you can do the following:

<%= f.input :category, :as => :select, :collection => option_groups_from_collection_for_select(ParentCategory.all, :categories, :name, :id, :name) %>

Reference from Rails API
Recommendation from Justin French (he mentions removing the :group_by)

查看更多
女痞
4楼-- · 2020-07-07 11:32

Hey, so I was trying to solve a particular problem with formtastic, and discovered that you can modify the query used to build the list by using :find_options. Looking at the code, I saw that if you don't specify :group_by, then the list of values ultimately comes down to a find(:all) on the model object (in a method called find_raw_collection_for_column in formtastic.rb). The parameters after the :all are the set of parameters specified by :find_options. Thus, you can apply any conditions or other parameters that you would normally use in find(*args) (see http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000333). Personally, I added this to my f.input statement:

:find_options => {:conditions => {:country_id => @business.country ? @business.country.id : nil}}

By doing this, I limited the select statement for the query to only the current county ID. But you should be able to use :group in a similar manner to specify who you wanted to GROUP BY in your query, like this:

:find_options => {:group => 'parent.id'}

Hopes this helps.

查看更多
登录 后发表回答