Mục tiêu của tôi là sử dụng đá quý nested_form: https://github.com/ryanb/nested_form nhưng thay vì chỉ tạo một nhóm nhãn và trường mới mỗi khi bạn thêm đối tượng, tôi muốn chèn hàng vào bảng hiện có .Rails nested_form thêm các mục vào hàng bảng
= nested_form_for @transaction do |f|
%h3 Line Items
%table
%tr
%th Branch
%th Department
%th Invoice #
%th Amount
%th Transaction Type
%th Deposit (y/n)
%th
= f.fields_for :line_items do |line_item|
%tr
%td
= line_item.text_field :location_id
%td
= line_item.text_field :department_id
%td
= line_item.text_field :invoice_num
%td
= line_item.text_field :amount
%td
= line_item.text_field :transaction_type
%td
= line_item.text_field :deposit
%td= line_item.link_to_remove "Remove"
%p= f.link_to_add "Add", :line_items
Nút .link_to_add chỉ tạo một loạt các trường trong hàng đầu tiên, td đầu tiên.
<h3>Line Items</h3>
<table>
<tr>
<th>Branch</th>
<th>Department</th>
<th>Invoice #</th>
<th>Amount</th>
<th>Transaction Type</th>
<th>Deposit (y/n)</th>
<th></th>
</tr>
<div class="fields"><tr>
<td>
<input id="transaction_line_items_attributes_0_location_id" name="transaction[line_items_attributes][0][location_id]" size="30" type="text" />
</td>
<td>
<input id="transaction_line_items_attributes_0_department_id" name="transaction[line_items_attributes][0][department_id]" size="30" type="text" />
</td>
<td>
<input id="transaction_line_items_attributes_0_invoice_num" name="transaction[line_items_attributes][0][invoice_num]" size="30" type="text" />
</td>
<td>
<input id="transaction_line_items_attributes_0_amount" name="transaction[line_items_attributes][0][amount]" size="30" type="text" />
</td>
<td>
<input id="transaction_line_items_attributes_0_transaction_type" name="transaction[line_items_attributes][0][transaction_type]" size="30" type="text" />
</td>
<td>
<input id="transaction_line_items_attributes_0_deposit" name="transaction[line_items_attributes][0][deposit]" size="30" type="text" />
</td>
<td><input id="transaction_line_items_attributes_0__destroy" name="transaction[line_items_attributes][0][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields" data-association="line_items">Remove</a></td>
</tr>
<td><a href="javascript:void(0)" class="add_nested_fields" data-association="line_items">Add</a></td>
</div>
</table>
Tôi đã thử đặt .link_to_add ở một vài nơi nhưng không đặt chúng vào hàng của riêng chúng.
Có cách nào dễ dàng để thêm khoảng một hàng hộp nhập liệu mỗi lần không?
Bạn có tệp 'views/_line_items_fields.html.haml' và nếu có, nó trông như thế nào? –