1

我有一种复杂的形式,我使用 ActiveModel 对象来验证一些通用字段:

  class FormReportPivot
   include ActiveModel::Validations
   include ActiveModel::Conversion
   extend ActiveModel::Naming

   attr_accessor :name, :pages, :columns, :rows, :table, 
   :pages_aggregation, :columns_aggregation, :rows_aggregation, :table_aggregation

   def initialize(attributes = {})
    attributes.each do |name, value|
      public_send("#{name}=", value)
    end
   end

   def persisted?
    false
   end

  end

我有这个字段的nested_form:

 = f.simple_fields_for :pivots do |pivots_builder|
  = render :partial => 'pivot_fields', :locals => { :pivots_builder => pivots_builder }
  = f.link_to_add "Add pivot", :pivots, model_object: FormReportPivot.new, class: "btn blue"

部分的:

  = pivots_builder.input :name, input_html: {:class => "m-wrap"}, 
    placeholder: t('.name')
  = pivots_builder.input :columns, collection: @form_report.columns_collection, :include_blank => false, input_html: {:class => "m-wrap chosen", multiple: true}
  = pivots_builder.input :rows, collection: @form_report.columns_collection, :include_blank => false, input_html: {:class => "m-wrap chosen", multiple: true}
  = pivots_builder.input :table, collection: @form_report.columns_collection, :include_blank => false, input_html: {:class => "m-wrap chosen", multiple: true}
  = pivots_builder.input :table_aggregation, collection: FormReport::AGGREGATING_FUNCTIONS, :include_blank => false, input_html: {:class => "m-wrap chosen"}

但是,当我单击“添加数据透视表”时,我会得到具有相同名称、相同 ID 且没有时间戳的相同字段:

 <input class="string optional m-wrap m-wrap" 
 id="form_report_pivots_attributes_name" 
 name="form_report[pivots_attributes][name]" placeholder="" title="">
4

1 回答 1

1

刚刚在 fields_for :pivots 中包含 fields_for :new_pivots 和嵌套表单用正确的时间戳替换 new_pivots :

形式:

  = f.simple_fields_for :pivots do |pivots_builder|
    = render :partial => 'pivot_fields', :locals => { :f => pivots_builder }
  = f.link_to_add "Add pivot", :pivots, model_object: FormReportPivot.new, class: "btn blue"

部分的:

  .well
    = f.simple_fields_for :new_pivots do |pf|
      = pf.input :name, input_html: {:class => "m-wrap"}, 
        placeholder: t('.name')
      = pf.input :columns, collection: @form_report.columns_collection, :include_blank => false, input_html: {:class => "m-wrap chosen", multiple: true}
      = pf.input :rows, collection: @form_report.columns_collection, :include_blank => false, input_html: {:class => "m-wrap chosen", multiple: true}
      = pf.input :table, collection: @form_report.columns_collection, :include_blank => false, input_html: {:class => "m-wrap chosen", multiple: true}
      = pf.input :table_aggregation, collection: FormReport::AGGREGATING_FUNCTIONS, :include_blank => false, input_html: {:class => "m-wrap chosen"}
于 2013-11-05T02:40:34.633 回答