我有一个父对象,其中有许多非常相似的子对象。我有一个带有表格的表单,可以使用 cocoon 来编辑它们。有了它(和这个答案 https://stackoverflow.com/questions/18734783/nested-form-cocoon-is-it-okay-to-use-table-rows-for-nested-fields),我可以向表中添加新行,并删除现有行,而且效果很好。
我想做的是在行末尾添加另一个按钮,与删除按钮一起,将该行克隆为新行。
家长表格:
= 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
行的元素:
= link_to_add_association 'Clone', f, :codes, data: {"association-insertion-node" => "tbody.codes", "association-insertion-method" => "append"}, render_options: {locals: {code: code.object}}
我有 2 个问题。首先,cocoon 预生成要插入的 HTML 的方式,我不能将此函数调用放在部分中,因为 - 即使我的语法正确 - 它也会创建一个无限循环。
如果有某种方法可以用当前对象的值“播种”HTML 生成,我很乐意通过某种方式将 DOM 节点放置在相应表行的最后一个单元格中来解决该问题。当我想插入“克隆”按钮时,我在code.object
多变的。 (作为code
此时是fields_for
形成对象。)
有什么办法可以做到这一点吗?render_options
看起来很有希望,但我无法弄清楚这是否只是一个语法问题,或者生成器是否永远不会查看我在生成字段时传递的值的哈希值。