1

我在那裏我使用加載ActiveModel對象用於驗證一些通用領域的一個複雜的形式:nested_form:所有添加的字段具有相同的名稱

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"} 

但是當我點擊「添加轉動」我得到相同的網絡連接視場與同名同身份證件和無時間戳:

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

回答

1

就包括fields_for:在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"} 
+0

Lifesaver。不知道爲什麼這是必要的,但哇它完全解決了我的問題,謝謝! –

相關問題