2011-09-24 67 views
3

請使用html_options顯示使用Cocoon的'link_to_add_association'的任何示例。 https://github.com/nathanvda/cocoonRails 3.1。需要Cocoon link_to_add_association示例

文件說:

html_options: extra html-options (see link_to) There are two extra options that allow to conrol the placement of the new link-data: 

    data-association-insertion-node : the jquery selector of the node 
    data-association-insertion-position : insert the new data before or after the given node. 

但我不明白的事,如果我想之前我的「添加元素」鏈接插入部分。不僅在父母的div開始之後。這不是要去工作:

<%= link_to_add_association 「添加元素」,F:production_years, :位置=> 「after_all」 %>

回答

9

我承認這是一個有點模糊的,也許我應該在代碼中提供一個例子。

在你application.js你應該寫這樣的:

$(document).ready(function() { 
    $("a.add_fields"). 
     data("association-insertion-position", 'before'). 
     data("association-insertion-node", 'this'); 
}); 

其中a.add_fields會選擇點擊的鏈接。我最近才更新了自述文件,並更好地解釋瞭如何處理插入操作。

希望這會有所幫助。

+0

+1 - 這是非常需要的。謝謝! – Jordan

+0

我認爲這是記錄良好的https://github.com/nathanvda/cocoon#control-the-insertion-behaviour(或至少比以前更好)。 – nathanvda

+0

就這樣。不知何故,我錯過了。感謝您指出。 – Jordan

5

如果您想用數據的屬性,你可以做些什麼來做到這一點:

<%= link_to_add_association 'Add Item', f, :items, :data => {"association-insertion-method" => "before" } %> 
+0

如果您使用':data =>'您不能使用jquery函數來獲取關聯插入節點。如 'data(「association-insertion-node」,function(link){return .closest('。row')。next('。row')。find('。sub_tasks_form') }); ' – DrevanTonder