【问题标题】:Rails 3.1. Cocoon link_to_add_association example needed导轨 3.1。需要 Cocoon link_to_add_association 示例
【发布时间】:2011-09-24 10:46:27
【问题描述】:

请展示使用 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.

但我不明白该怎么做,如果我想在我的“添加元素”链接之前插入部分内容。不仅仅是在父母的 div 开始之后。这行不通:

"after_all" %>

【问题讨论】:

    标签: forms ruby-on-rails-3 cocoon-gem


    【解决方案1】:

    我承认这有点晦涩,也许我应该在代码中提供一个示例。

    在您的application.js 中,您应该这样写:

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

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

    希望这会有所帮助。

    【讨论】:

    【解决方案2】:

    如果你想使用数据属性来做到这一点,你可以这样做:

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

    【讨论】:

    • 如果你使用:data =&gt;你不能使用jquery函数来获取关联插入节点。如data("association-insertion-node", function(link){ return link.closest('.row').next('.row').find('.sub_tasks_form') });
    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2012-02-06
    • 2014-06-09
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多