Rails 3.1.需要Cocoon link_to_add_association示例

Baz*_*zZy 3 forms ruby-on-rails-3 cocoon-gem

请显示使用Cocoon的'link_to_add_association'和html_options的任何示例. https://github.com/nathanvda/cocoon

文件说:

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.
Run Code Online (Sandbox Code Playgroud)

但我无法理解该怎么做,如果我想在我的"添加元素"链接之前插入部分.不仅在父母的div开始之后.这不会起作用:

<%= link_to_add_association"add element",f,:production_years,:position =>"after_all"%>

nat*_*vda 9

我承认这有点模糊,也许我应该在代码中提供一个例子.

在你的application.js,你应该写类似:

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

a.add_fields将在哪里选择您的可点击链接.我刚刚更新了自述文件,更好地解释了如何处理插入.

希望这可以帮助.


pat*_*vey 6

如果您想使用数据属性来执行此操作,您可以执行以下操作:

<%= link_to_add_association 'Add Item', f, :items, :data => {"association-insertion-method" => "before" } %>
Run Code Online (Sandbox Code Playgroud)