Skip to main content
1 of 2

You can try this, taken from Ruby on Rails API:

quantities_fields.html.erb:

<fieldset class="nested">
   <legend>Ingredienti</legend>
   <ol id='ingredients'> 
    <%= render :partial => 'dummy_hidden_fields' %>

    <%= fields_for :quantities do |recipe_quantities| %>
    <li class="fields">
        <%= recipe_quantities.select :ingredient_id, options_from_collection_for_select(ingredients_for_select, :id, :name_unit, recipe_quantities.object.ingredient_id) %>
        
        ...
        
        <%#  fields_for will automatically generate a hidden field to store the ID of the record, so the following line is not necessary %>
        <%#= recipe_quantities.hidden_field_tag 'recipe[quantities_attributes][][id]', dq.id %>  


        <%# when you use a form element with the _destroy parameter, with a value that evaluates to true, you will destroy the associated model (eg. 1, ‘1’, true, or ‘true’) %>
        <%= recipe_quantities.hidden_field :_destroy, 0  %>
        <%= link_to_function "elimina", "remove_ingredient(this)", :class => 'btn btn-mini' %>       
    </li>   
    <% end %> 
</ol> 
<%= link_to_function "Add ingredient", "add_ingredient()" %>