【问题标题】:sorting accepts_nested_attributes_for in formtastic在 formtastic 中排序accept_nested_attributes_for
【发布时间】:2012-08-29 17:57:09
【问题描述】:

我正在尝试使用 formtastic 来呈现嵌套表单。我的父模型中有 has_many/accepts_nested_attributes_for 设置。一切都很好。唯一的问题是我想对嵌套模型的顺序进行排序。

# this works but i want answers sorted a certain way
= semantic_form_for survey do |f|
  = f.inputs :for => :answers do |answer_form|
    = answer_form.input :content

如果我尝试做类似的事情:

# form styles become extremely messed up but the order is correct
= semantic_form_for survey, do |f|
  = f.semantic_fields_for :answers, f.object.answers.joins(:question).order('questions.position') do |answer_form|
    = answer_form.input :content

我什至尝试使用 :finder_sql 和 :class 创建一个名为 :sorted_answers 的“假” has_many 关系,但这也不起作用(answer_form 为 nil IIRC)。

我想我要问的是我是否可以使用 :for => (关系)但指定关系的顺序。也许使用:for_options?

【问题讨论】:

    标签: ruby-on-rails nested-attributes formtastic


    【解决方案1】:

    我遇到了和你一样的情况,这是我的解决方案:

    你应该有一个名为“Answer”的模型,在 default_scope 中设置顺序:

    class Answer < ActiveRecord::Base
    
      default_scope :order => "id"
    
    end
    

    【讨论】:

      【解决方案2】:

      使用 formtastic 3.1.3,这对我有用

      f.input :answers, as: :check_boxes, :member_label => :some_column, collection: Answer.order(:some_column)
      

      需要注意的是,集合必须是 ActiveRecord_Relation 类而不是数组,因为 Formtastic 是 activerecord-ish

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        • 1970-01-01
        • 1970-01-01
        • 2012-11-23
        • 1970-01-01
        相关资源
        最近更新 更多