2013-04-03 10 views
0

我有下面的代碼,以我的發票模型中創建一個嵌套模型(invoice_line_items):使用nested_form自舉表中沒有定型正常

= nested_form_for @invoice, mutipart: true, class: "form-horizontal" do |f| 

    ... 

    %table.table.table-bordered{:id => "line-items"} 
     %thead 
     %tr 
      %th 
      %th Description 
      %th Quantity 
      %th Rate 
     %tbody 
     %tr 
      = f.fields_for :invoice_line_items do |line_item| 
      %td= line_item.link_to_remove "X" 
      %td= line_item.text_field :description 
      %td= line_item.text_field :quantity, :class => "input-mini" 
      %td= line_item.text_field :rate, :class => "input-mini" 
    = f.link_to_add "Add", :invoice_line_items, :data => { :target => "#line-items" } 

我有兩個問題:1)當我添加一個新行通過點擊「添加」....它與表格格式不匹配,並且不會插入到表格中。我嘗試了一切來讓它起作用,但它卻沒有。我也嘗試添加ryanb在他的寶石文檔中提到的「:target」。 2)我希望最初有3個invoice_line_items爲發票頁面上的用戶準備好,但我不知道如何執行此操作。

編輯:我現在有點不同了,因爲我一直在玩它。我不認爲我做是正確的還是,但現在它創建了一個新的形式,每次我點擊「添加」:

.row-fluid 
    = f.fields_for :invoice_line_items, :wrapper => false do |line_item| 
    %table.table.table-bordered#tasks 
     %thead 
     %th 
     %th Description 
     %th Quantity 
     %th Rate 
     %tr.fields 
     %td= line_item.link_to_remove "X" 
     %td= line_item.text_field :description 
     %td= line_item.text_field :quantity, :class => "input-mini" 
     %td= line_item.text_field :rate, :class => "input-mini" 
    .row-fluid 
    = f.link_to_add "Add", :invoice_line_items, :data => { :target => "#tasks" } 

回答

1

這是一個艱難的一個搞清楚,但事實證明,最近更新nested_form gem的版本不包含jquery_nested_form.js文件的最新版本(儘管文檔中提到使用此函數)。因此,爲了得到這個工作,我不得不把它添加到我的application.js:

$(function ($) { 
    window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) { 
    var target = $(link).data('target'); 
    if (target) { 
     return $(content).appendTo($(target)); 
    } else { 
     return $(content).insertBefore(link); 
    } 
    }; 
}); 

這個改寫在jquery_nested_form.js功能,現在一切正常。

相關問題