Search code examples
ruby-on-railsrubycocoon-gem

My nested cocoon form only accepts the last line of my dynamic table


I am currently creating a nested form using cocoon. I have a table that takes in information for everyone in a household. the form dynamically creates a new line when the user clicks on add new member. The problem that I am having is that when the form is submitted, only the last line of the table is passed through.

I have tried many different things including changing the attributes of the members and also trying to isolate just the household members so I can see what is happening. I have tried giving them unique id's, but that didn't work. It might be something to do with the rails sever or just some bad code that I can't see.

Here is where I am nesting the form

 <tbody class="members">
    <%= f.fields_for :household_members do |ff|%>
      <%= render 'household_member_fields', :f => ff %>
      <%end%>
    </tbody>
  </table>
  <%= link_to_add_association 'Add household member', f, :household_members, data: {association_insertion_node: '.members',
      association_insertion_method: :append} %>

This is the beginning of the households_member_fields:

<tr class= "nested-fields">
  <% f.hidden_field :id%>
  <td><% f.label(:name)%>
    <%= text_field(:household_members, :name) %></td>
  <td><% f.label(:birthdate)%>
    <%= date_field(:household_members, :birthdate) %></td>
  <td><% f.label(:ssn)%>

This is my controller

  def create
    addresses = params[:addresses].permit([:county]).to_h
    contact_info = params[:contact_info].permit(params[:contact_info].keys).to_h
    household = params[:household_type].permit([:house_type]).to_h
    household_members = member_params
    @workflow = CreatesUser.new(address_info: addresses,
      contact: contact_info, household: household)
    @workflow.create
    redirect_to users_path
  end

  private

  def member_params
    params.require(:user).permit(household_members_attributes: [:id, :name, :birthdate, :ssn, :gender, :hispanic, :race, :or_tribe, :education, :_destroy])
  end

When I currently submit two or more household members I only ever get one of them like this:

"household_members"=>{"name"=>"Fake2", "birthdate"=>"2019-07-21", "ssn"=>"fake2", "gender"=>"Female", "hispanic"=>"0", "race"=>"Alaska Native", "or_tribe"=>"0", "education"=>"Some college"}, "disablility_assistances"=>{"disabled"=>"0", "homebound"=>"0", "snap"=>"0", "ohp"=>"0", "med_insurance"=>"fake2"}, "veteran_statuses"=>{"veteran"=>"0"}

I am expecting to get multiple of these. If you have any incite into what I am doing wrong I would greatly appreciate it!

Thanks,

Aala95

After looking at what my code is returning a little more it looks like the nested form is being submitted but only with an ID and delete: "user"=>{"household_members_attributes"=>{"0"=>{"_destroy"=>"false"}, "1"=>{"_destroy"=>"false"}}}


Solution

  • You have to fix your nested items partial: remove the scoped name --now it will be double scoped, this explains why some attributes are in household_members (your scope and blocked by the strong parameters definition) and some in household_members_attributes (the expected scope). Also there is no need to add the hidden field :id (that will be automatically handled by the index in the array).

    So write your partial as follows:

    <tr class= "nested-fields">
      <td><% f.label(:name)%>
        <%= text_field(:name) %></td>
      <td><% f.label(:birthdate)%>
        <%= date_field(:birthdate) %></td>
      <td><% f.label(:ssn)%>