本文介绍了分组形式选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在有了Formtastic,我可以简单地选择:

Now with Formtastic I have plain select:

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

在这里,我仅显示子类别.我将 acts_as_tree 插件用于父子关系.我也想显示父类别.

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>

如何在Formstastic select中为具有acts_as_tree功能的模型使用分组?有人知道吗

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

已更新

我发现这应该可行:

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

但没有错误:

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

看起来Formtastic中存在一些错误.我浏览了formtastic.rb,并在 detect_group_association 方法中找到了object_class:

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

实际上是在此方法中未定义的 object_class ,formtastic.rb中没有使用该名称的privat方法.但是我们可以使用:group_association 明确定义关联.

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

我尝试关闭 Acts_as_tree 并编写自己的自引用关联.与Acts_as_tree作品相同的是:

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?

已更新

下一步.这段没有Formtastic的代码可以正常工作:

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是具有父类别集合的帮助方法.

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

最后一件事是将其翻译为Formtastic语法:)

The last thing is translate it into Formtastic syntax :)

推荐答案

如果有人遇到相同的问题,则可以执行以下操作:

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

从Rails API参考
来自贾斯汀·法文的建议(他提到删除:group_by)

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

这篇关于分组形式选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 12:08