Formtastic选择分组

Vol*_*ldy 8 grouping ruby-on-rails acts-as-tree optgroup formtastic

现在使用Formtastic我可以选择:

= f.input :category, :as => :select, :include_blank => false, :collection => subcategories
Run Code Online (Sandbox Code Playgroud)

这里我只展示儿童类别.我使用acts_as_tree插件进行父子关系.我想展示父类别.

Formtastic生成的选择应该如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

如何在Formtastic中使用分组选择具有acts_as_tree功能的模型?有人知道吗?

更新

我发现这应该有效:

= f.input :category, :include_blank => false, :group_by => :parent
Run Code Online (Sandbox Code Playgroud)

但它没有错误:

undefined local variable or method `object_class' for #<Formtastic::SemanticFormBuilder:0x87d3158>
Run Code Online (Sandbox Code Playgroud)

看起来Formtastic中有一些错误.我查看了formtastic.rb并在detect_group_association方法中找到了object_class :

  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
Run Code Online (Sandbox Code Playgroud)

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

- 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
Run Code Online (Sandbox Code Playgroud)

但我遇到了另一个错误:

undefined method `children' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)

我试着关掉Acts_as_tree并编写我自己引用的自我约束.与Acts_as_tree相同的工作应如下所示:

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
Run Code Online (Sandbox Code Playgroud)

错误是一样的.有人可以帮忙吗?

更新

下一步.没有Formtastic的代码工作正常:

= grouped_collection_select('', :category_id, top_categories, :children, :name, :id, :name, :include_blank => true)
Run Code Online (Sandbox Code Playgroud)

ps:top_categories是带有父类别集合的辅助方法.

最后一件事就是把它翻译成Formtastic语法:)

Dem*_*tor 16

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

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

参考来自
Justin French的Rails API 建议书(他提到删除:group_by)

  • 我有一个问题,它不会自动选择相关的类别.你遇到过同样的问题吗? (6认同)