2016-08-11 43 views
2

我正在使用cocoon添加嵌套的屬性。我在我的觀點如下代碼:無法在表中添加字段與繭

<table> 
    <thead> 
    <tr> 
    <th>Title</th> 
    <th>Action</th> 
    </tr> 
    </thead> 
    <tbody id="emergency_personnels"> 
    <%= f.fields_for :emergency_personnels do |emergency_personnel| %> 
     <%= render 'emergency_personnel_fields', f: emergency_personnel %> 
    <% end %> 
    </tbody> 
</table> 
<%= link_to_add_association 'Add emergency personnel', f, :emergency_personnels, data: {"association-insertion-node" => "tbody#emergency_personnels", "association-insertion-method" => "append"} %> 

它應該工作正常,但新的字段不表,而不是出現在表外的TBODY下追加。

任何人都可以提出一個解決方案。由於

回答

1

按繭文檔: insertion-behaviour

默認行爲

默認插入位置是在當前容器的後面。

如果您要添加到自定義位置,那麼我們可以通過兩種數據屬性實現這一點:

association-insertion-method在何處添加它

association-insertion-node那裏將它添加在關係與節點

這裏是例子:

$(document).ready(function() { 
    $("#owner a.add_fields"). 
     data("association-insertion-method", 'append'). 
     data("association-insertion-node", '#parent_table'); 
}); 

注:如果你想模板添加到特定位置,這是

  • 不是鏈接
  • 鏈接直接父母或兄弟姐妹中多次出現 - 爲實例,在深度嵌套形式內

您需要指定association-insertion-node作爲函數。例如

$(document).ready(function() { 
    $(".add_sub_task a"). 
     data("association-insertion-method", 'append'). 
     data("association-insertion-node", function(link){ 
     return link.closest('.row').next('.row').find('.sub_tasks_form') 
     }); 
}); 
+0

它與一些調整工作。謝謝 –

+0

請分享調整....我會更新我的答案 –