2010-01-28 69 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'}

希望這有助於。