Rails form not properly setting checked fields_for rows as checked, on submit, in controller params

63 views Asked by At

I've been bangin my head over this for a bit.. I have a form, and I'm utilizing fields_for for the PurchaseOrderLineItem associations of a PurchaseOrder

<%= form_with model: [@quote, @purchase_order], local: true do |form| %>
  <% if purchase_order.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(purchase_order.errors.count, "error") %> prohibited this purchase_order from being saved:</h2>

      <ul>
        <% purchase_order.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="text-center text-xl text-bold">
    <%= form.label :number, 'PO Number' %>
    <%= form.text_field :number, class: 'text-field-input' %>
  </div>
  <table>
    <tr>
      <th class='px-4 py-2'>Include in PO</th>
      <th class='px-4 py-2'>Part No.</th>
      <th class='px-4 py-2'>Description</th>
      <th class='px-4 py-2'>Condition</th>
      <th class='px-4 py-2'>Total Quantity Requested</th>
      <th class='px-4 py-2'>Quantity Awarded (So Far)</th>
      <th class='px-4 py-2'>Quantity To Award</th>
      <th class='px-4 py-2'>Quantity Quoted</th>
      <th class='px-4 py-2'>Unit Price</th>
      <th class='px-4 py-2'>Total Price</th>
    </tr>
    <% @quote_line_items.each do |line_item| %>
      <%= form.fields_for 'purchase_order_line_items[]', line_item do |cf| %>
        <%= cf.hidden_field :quote_line_item_id, value: line_item.id %>
        <%= cf.hidden_field :id, value: line_item.po_line_item&.id %>
        <tr class='border px-4 py-2 h-12' data-line-item-id=<%= line_item.id %>>
          <td class='border px-4 py-2'><%= cf.check_box(:being_awarded, {class: 'quote-award-checkbox', checked: !!line_item.po_line_item}) %></td>
          <td class='border px-4 py-2'><%= line_item.part.number %></td>
          <td class='border px-4 py-2'><%= line_item.part.description %></td>
          <td class='border px-4 py-2'>
            <%= select_tag(
              :part_condition,
              options_for_select(part_conditions_for_select_options,
                selected: part_conditions_for_select_options[line_item.part_condition]
              )
            ) %>
          </td>
          <td class='border px-4 py-2'><%= line_item.rfq_line_item.quantity %></td>
          <td class='border px-4 py-2'><%= line_item.rfq_line_item.quantity_fulfilled %></td>
          <td class='border px-4 py-2'><%=
            cf.number_field(
              :quantity,
              {
                  value: line_item.po_line_item&.quantity || 0,
                  min: 0, max: line_item.max_allowed_quantity,
                  class: 'awarded-quantity-input number-field-input'
              })
          %></td>
          <td class='border px-4 py-2 quantity'><%= line_item.quantity %></td>
          <td class='border px-4 py-2'><%=
            cf.number_field(
              :unit_price,
              {
                  value: line_item.po_line_item&.unit_price || line_item.unit_price,
                  step: 0.01,
                  class: 'number-field-input unit-price'
              }
            )
          %></td>
          <td class='border px-4 py-2 total-price'><%= line_item.total_price %></td>
        </tr>
      <% end %>
    <% end %>
  </table>

  <div class="actions text-center m-4">
    <%= form.submit 'Submit', class: 'big-btn px-8 py-4' %>
  </div>
<% end %>

The problem here is the child form checkboxes are not sending the expected values to the controller

The being_awarded attribute, comes in mostly as "unchecked" and sometimes [at random] will mark some of the being_awarded checkboxes as "checked"

being_awarded is defined as an attr_accessor of PurchaseOrderLineItem

1

There are 1 answers

0
gemart On

It's the little mistakes that drive us the most crazy...

I started the loop outside of the child form, thereby calling fields_for for every line_item and it was just causing erratic behavior