如何使用 cocoon 在 Rails 中克隆子对象? [英] How could I clone a child object in Rails using cocoon?

查看:57
本文介绍了如何使用 cocoon 在 Rails 中克隆子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父对象,其中包含许多非常相似的子对象.我有一个带有表格的表格,可以使用 cocoon 编辑它们.有了它(和 这个答案),我可以向表中添加新行,并删除现有的行,它工作得很好.

I have a parent object with scores of children that are all very much alike. I have a form, with a table, to edit them using cocoon. With it (and this answer), I can add new rows to the table, and remove existing ones, and it works just fine.

我想要做的是在行的末尾添加另一个按钮,与删除按钮一起,将该行克隆为新行.

What I'd like to do is add another button at the end of the row, to go along with the deletion button, to clone that row as the new row.

父表单:

= simple_form_for @release, html: { multipart: true } do |f|
  = f.error_notification
  .col-md-12
    %table#myTable.table-striped.table-bordered
      %thead
        %tr
          %th Description
          ...
          %th Remove

      %tbody.codes
        = f.simple_fields_for :codes, wrapper: false do |code|
          = render 'code_fields', f: code
          ---> New code below would go here <---

    .links
      = link_to_add_association 'Add Code', f, :codes, data: {"association-insertion-node" => "tbody.codes", "association-insertion-method" => "append"}

    .form-actions
      = f.button :submit, class: 'btn btn-primary pull-right'

子部分:

%tr.nested-fields
  = f.input :id, as: :hidden
  %td= f.input :description, label: false, input_html: { class: 'input-sm myinput' }
  ...
  %td
    = link_to_remove_association f, class: 'btn btn-default btn-xs' do
      .glyphicon.glyphicon-remove

我希望能够将这样的内容添加到该行的最后一个 td 元素:

I'd like to be able to add something like this to the last td element of the row:

= link_to_add_association 'Clone', f, :codes, data: {"association-insertion-node" => "tbody.codes", "association-insertion-method" => "append"}, render_options: {locals: {code: code.object}}

我有两个问题.首先,cocoon 预先生成要插入的 HTML 的方式,我不能把这个函数调用放在部分中,因为——即使我的语法正确——它会创建一个无限循环.

I have 2 issues. First, the way cocoon pre-generates the HTML to insert, I can't put this function call in the partial, because -- even if I were to get the syntax right -- it would create an infinite loop.

如果有某种方法可以用当前的值播种"HTML 生成,我很乐意通过某种方式将 DOM 节点通过 jQuery 将 DOM 节点放到相应表格行的最后一个单元格中来解决该问题目的.在我想插入克隆"按钮时,我在 code.object 变量中有当前的代码"子对象.(此时的 codefields_for 表单对象.)

I'd be happy to work-around the problem by somehow jQuery-ing DOM nodes into place in the last cell of the appropriate table row if there were some way to "seed" the HTML generation with the values of the current object. At the point I want to insert the "clone" button, I have the current "code" child object in the code.object variable. (As code at this point is the fields_for form object.)

有没有办法做到这一点?render_options 看起来很有希望,但我不知道这是否只是一个语法问题,或者生成器是否永远不会查看我在生成字段时传递的值的哈希值.

Is there any way to do this? render_options seems promising, but I can't figure out if this is just a syntax issue, or if the generator would never look at the hash of values I pass it in generating the fields.

推荐答案

看看:wrap_object 选项,它允许预初始化对象,我认为您也应该能够使用它从现有对象克隆.

Take a look at the :wrap_object option, it allows to pre-initialize the object, I think you should be able to use it to clone from an existing object as well.

例如这样的事情可能会奏效:

E.g. something like this might work:

- code_to_clone = f.object
= link_to_add_association('Clone', f, :codes,
    wrap_object: Proc.new { |new_code| new_code.name = code_to_clone.name; new_code })

仅复制 1 个字段用于演示目的 :)

Just copying 1 field for demonstration purposes :)

这篇关于如何使用 cocoon 在 Rails 中克隆子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆