2010-01-28 68 views
7

现在选择我有平原选择:Formtastic与Formtastic分组

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

在这里,我只显示儿童的类别。我使用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> 

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

修订

我想通了,这应该工作:

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

,但它不会出现错误:

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

它看起来像有在Formtastic一些bug 。我已经通过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 

事实上object_class不确定这个方法并没有在formtastic.rb该名称没有普里瓦方法。但是我们可以使用: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 

,但我遇到了另一个错误:

undefined method `children' for nil:NilClass 

我试着开关等关Acts_as_tree和写我自己的自我引用assositions。与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 

错误是一样的。任何人都可以帮忙吗?

更新

下一个小小的一步。此代码没有Formtastic工作正常:

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

p.s:top_categories是辅助方法与父类别的集合。

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

+0

你有任何运气得到这个在Formtastic中正常工作? – Codebeef 2010-03-10 10:59:49

+0

还没有。我在github向Formtastic的人提交了一个问题。没有关于分组选项的规范覆盖范围,并且将不胜感激。 – Voldy 2010-03-10 13:42:00

+0

你知道了吗? – 2011-10-18 12:51:30

回答

14

如果任何人有同样的问题,你可以做到以下几点:

<%= 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(他提到卸下:GROUP_BY)

+6

我有一个问题,它不会自动选择关联的类别。你有没有遇到同样的问题? – Cristian 2013-01-09 20:18:28

+1

仅供参考:通过Google来到这里的其他人:要自动选择关联的类别,只需按照http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i传递ID作为第6个参数-option_groups_from_collection_for_select – Adam21e 2013-07-23 23:17:44

+0

@ Adam21e我试着添加:id作为第6次的征途,但无济于事。它会自动选择列表中的第一个选项。 – 2014-03-05 04:52:29

0

为什么不尝试建设一个辅助的项目列表,然后通过该进入选择标签?

对于formtastic或acts_as_tree我不太了解,但是如果您在上述工作中遇到问题,我认为在将它们传递给您的选择之前先构建您的选项是非常合理的。

+2

这工作正如我更新我的问题。但是我使用Formtastic并希望使用它的语法。 – Voldy 2010-01-29 12:47:44

1

嘿,所以我试图用formtastic来解决一个特定的问题,并发现你可以通过使用find_options来修改用于构建列表的查询。看看代码,我发现如果你没有指定:group_by,那么值列表最终归结为模型对象上的find(:all)(在formtastic.rb中名为find_raw_collection_for_column的方法中)。 :all后面的参数是由find_options指定的一组参数。因此,您可以应用通常在find(* args)中使用的任何条件或其他参数(请参阅http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000333)。我个人将其添加到我的f.input声明中:

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

通过这样做,我将查询的select语句限制为仅当前的县ID。但是,你应该能够使用:以类似的方式组来指定你想GROUP BY谁在你的查询,例如:

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

希望这有助于。