समूहीकरण अब Formtastic साथ साथ चयन मैं सादे चयन किया है:Formtastic
= f.input :category, :as => :select, :include_blank => false, :collection => subcategories
यहाँ मैं केवल बच्चों श्रेणियों दिखा। मैं अभिभावक-बाल संबंधों के लिए कृत्यों_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>
कैसे acts_as_tree कार्यक्षमता के साथ Formtastic में समूहीकरण मॉडल के लिए चयन उपयोग कैसे करें? क्या कोई जानता है?
UPDATED
मैं पता लगा है कि इस काम करना चाहिए:
= 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 पाया:
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 बंद swith की कोशिश की और अपने ही आत्म संदर्भित 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
त्रुटि समान है। क्या कोई मदद कर सकता है?
अगला छोटे कदम अपडेट किया गया। फॉर्मेटास्टिक काम के बिना यह कोड ठीक है:
= grouped_collection_select('', :category_id, top_categories, :children, :name, :id, :name, :include_blank => true)
p.s: top_categories मूल श्रेणियों के संग्रह के साथ सहायक विधि है।
आखिरी बात Formtastic वाक्य रचना :) में इसका अनुवाद है
आप को यह हो रही है के साथ किसी भी भाग्य किया है Formtastic में ठीक से काम करते हैं? – Codebeef
अभी तक नहीं। मैंने जिथब में फॉर्मेटस्टिक लोगों को एक मुद्दा जमा कर दिया है। समूहबद्ध विकल्पों के लिए कोई स्पेक कवरेज नहीं है और एक असफल नमूना की सराहना की जाएगी। – Voldy
क्या आपको यह पता चला? –