Domanda

I have a nested form, and within that another one. Basically what I have is a bunch of Ingredients and for each ingredient I have a quantity and unit which is stored in an association table between Entry and Ingredient called EntryIngredient. With my following JS, I'm able to add dynamic fields, but when I submit, I'm getting AssociationTypeMismatch error. Not sure why, the parameters look good to me ("ingredients_attributes"=>{"0"=>{"name"=>"Salt", "entry_ingredients"=>{"quantity"=>"2.5", "unit"=>"tbspns"}, "_destroy"=>""}}}}), what am I missing? Actually, I think it might be because its sending as entry_ingredients instead of entry_ingredients_attributes, but I don't see what I did differently in the second nested form. Here is the first form that has the fields for ingredients:

  <div id="ingredients">
      <p>Ingredient List:</p>
      <%= f.fields_for :ingredients, @entry.ingredients.build do |builder| %>
      <%= render 'ingredient_fields', :f => builder %>
      <% end %>
  </div>
  <div id='add_ingredient'>Add Ingredient</div>
  <div class="actions">
    <%= f.submit %>

here is the code for entry_ingredients:

<ul id="ingredient_list">
    <li>
      <%= f.label :name %>
      <%= f.text_field :name, :class => "ingredient_field" %>
      <%= f.hidden_field :_destroy, :class => "delete_ingredient" %>
        <%= f.fields_for :entry_ingredients, @entry.entry_ingredients.build do |builder| %>
        <%= render 'entry_ingredient_fields', :f => builder %>
        <% end %>
      <%= link_to "Remove", "#", :class => "remove_fields" %>
    </li>
</ul>

and heres my JS for adding a new ingredient dynamically:

$('form').on('click', '#add_ingredient', function(){
    count = $('#ingredient_list li').length;
    field = $('#ingredient_list li').first()
        .clone()                                    //clone the first element in the list
            .find('input')                          //find all its inputs
                .val('')                            //set all input values to blank
                    .end()
                        .find($('.ingredient_field'))
                            .prop({id: 'entry_ingredients_attributes_' + count + '_name', name: 'entry[ingredients_attributes][' + count +'][name]'  })
                                .end()
                                    .find($('.delete_ingredient'))
                                        .prop({id: 'entry_ingredients_attributes_' + count + '__destroy', name: 'entry[ingredients_attributes][' + count +'][_destroy]', value: 'false'  })
                                            .end()
                                                .find($('.ingredient_quantity'))
                                                    .prop({id: 'entry_ingredients_attributes_' + count + '_entry_ingredients_quantity', name: 'entry[ingredients_attributes][' + count +'][entry_ingredients][quantity]'})
                                                        .end()
                                                            .find($('.ingredient_unit'))
                                                                .prop({id: 'entry_ingredients_attributes_' + count + '_entry_ingredients_unit', name: 'entry[ingredients_attributes][' + count +'][entry_ingredients][unit]'})
                                                                    .end();
    $('#ingredient_list').append(field);
})
È stato utile?

Soluzione

The form helpers inspect the relationship between your models to figure out how to submit parameters. Did you include accepts_nested_attributes_for :entry_ingredients in the Ingredient model?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top