2011-09-24 70 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