mercredi 24 juin 2015

Rails nested form checkboxes not holding values

I'm building a payment system where a user can enter items in a receipt and select other users (via check-boxes), that are required to pay for the item.

So shopping_list has many shopping_items.

My problem is that when I go to edit a list, the checkboxes are not checked as they should be:

 <%= f.fields_for :shopping_items, :wrapper => false do |k| %>
      <tr class="fields">
        <td><%= k.text_field :memo, class: "form-control", type: 'text' %></td>
        <td><%= k.number_field :price, :step => 'any', class: "form-control", type: 'number' %></td>
        <td style="text-align:center;vertical-align:middle;">
          <!--<%= k.check_box :payees, { :multiple => true }, current_user.id, nil%>-->
          <%= k.object.payees %>
        </td>
        <% User.other_users(current_user).each do |u| %>
          <td style="text-align:center;vertical-align:middle;">
            <%= k.check_box :payees, { :multiple => true }, u.id,(k.object.payees.present? and k.object.payees.include?(u.id.to_s)) %>
          </td>
        <% end %>
        <td><%= k.link_to_remove "Remove this task" %></td>
      </tr>
    <% end %>

What I am trying to do is get the checkbox to check if payees contain a user's id, and check if that is true.

What happens is Rails throws an error saying k.object.payees is nil (hence the present?), but this conditions means no user is never selected.

I know payees is not nil, because right above the checkboxes I print out:

<%= k.object.payees %> => ["34","45","67"]

Which prints out correctly.

What is the correct syntax in this case?




Aucun commentaire:

Enregistrer un commentaire