代码之家  ›  专栏  ›  技术社区  ›  Voldy

带分组的formtastic select

  •  8
  • Voldy  · 技术社区  · 16 年前

    现在有了formtastic,我有了简单的选择:

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

    这里我只显示儿童类别。我使用 Acsthasast-树 父子关系插件。我也要显示父类别。

    formtastic生成的select应该如下所示:

    <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 select中,如何使用分组作为具有树功能的模型?有人知道吗?

    更新的

    我发现这应该有效:

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

    但不是错误的:

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

    看来formtastic里有一些虫子。我已经浏览了formtastic.rb并在 检测组关联 方法:

      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
    

    的确 对象类 此方法中未定义,并且formtastic.rb中没有该名称的privat方法。但是我们可以用 :组关联 明确定义关联。

    - 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
    

    我试着戒掉 Acsthasast-树 写下我自己的参考资料。与“作为树工作”相同,应如下所示:

    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是父类别集合的helper方法。

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

    3 回复  |  直到 13 年前
        1
  •  15
  •   Demelziraptor    13 年前

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

    <%= 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 (他提到删除:分组依据)

        2
  •  1
  •   Earl Jenkins    16 年前

    嘿,所以我试图用formtastic解决一个特定的问题,发现您可以使用:find_选项修改用于构建列表的查询。查看代码,我发现如果不指定:group_by,那么值列表最终会在模型对象上找到(:all)(在formtastic.rb的find_raw_collection_for_column方法中)。:all后面的参数是:find_选项指定的一组参数。因此,可以应用find(*args)中通常使用的任何条件或其他参数(请参见 http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000333 )。就我个人而言,我在f.input语句中添加了以下内容:

    :查找“选项”=>:条件=>:国家/地区_id=>@business.country?@business.country.id:无

    通过这样做,我将查询的select语句仅限于当前的County ID。但是您应该能够使用:以类似的方式分组,以指定在查询中要分组的对象,如下所示:

    :find_options=>:group=>'parent.id'

    希望这有帮助。

        3
  •  0
  •   theIV    16 年前

    为什么不尝试在助手中构建项目列表,然后将其传递到select标记中?

    我对formtastic不太了解,也不太了解tree,但如果你无法按照上面的方式进行操作,我认为在将选项传递给你的选择之前,先建立选项是完全合理的。

    推荐文章