是的。如果您要输入表格数据,那么您应该使用表格。
我尚未测试以下示例,但我认为它应该有效。假设您正在创建包含一堆行项目的收据,每个项目都有描述和价格。在您看来,要做的是:
%table
%thead
%tr
%th Description
%th Price
%tbody.line_items
= f.simple_fields_for :line_items do |f|
= render 'line_item_fields', f: f
.links
= link_to_add_association "Add", f, :line_items, data: {"association-insertion-node" => "tbody.line_items", "association-insertion-method" => "append"}
关联插入节点:这控制在哪里插入新的 line_item。在示例中我使用表体。
关联插入方法:用于插入新 line_item 的 jQuery 方法。在示例中,我将其附加到表主体的末尾。
在 _line_item_fields.html.haml 中:
%tr.nested-fields
%td= f.input :description, label: false
%td= f.input :price, label: false
%td= link_to_remove_association "Remove"
The .嵌套字段类很重要,因为它告诉 cocoon 当单击“删除”链接时要删除哪个元素。