<%= error_messages_for @ingredient %>

<%= f.text_field :name, size: 40, required: true %>

<%= f.number_field :ref_amount, size: 8, required: true, min: 0 %> <%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>

<%= f.select :group, group_options, required: true %>

<% @ingredient.nutrients.each_with_index do |n, index| %> <%= f.fields_for :nutrient, n, index: '' do |ff| %>

<%= ff.select :quantity_id, quantity_options, label: (index > 0 ? '' : :field_nutrients) %> <%= ff.number_field :amount, {size: 8, label: ''} %> <%= ff.select :unit_id, unit_options, label: '' %> <%= ff.check_box :_destroy, {style: "display:none", label: ''} %> <%= link_to t(".button_delete_nutrient"), '#', :class => 'icon icon-del', :style => "display:none", :onclick => "deleteNutrient(); return false;" %>

<% end %> <% end %>

<%= link_to t(".button_add_nutrient"), '#', :class => 'icon icon-add', :onclick => 'addNutrient(); return false;' %>

<%= javascript_tag do %> function addNutrient() { var row = $('p.nutrient:visible:last'); var new_row = row.clone().insertAfter(row); new_row.find('input[id$=_amount], select[id$=_quantity_id]').val(''); new_row.find('select[id$=_unit_id]').val(row.find('select[id$=_unit_id]').val()); new_row.find('label:first').text(''); if ($('p.nutrient:visible').length > 1) { $('p.nutrient a.icon-del').show(); } } function deleteNutrient() { var row = $(event.target).closest('p.nutrient'); row.find('[id$=_destroy]').val(1); row.hide(); $('p.nutrient:visible:first label:first').text('<%= t "field_nutrients" %>'); if ($('p.nutrient:visible').length <= 1) { $('p.nutrient a.icon-del').hide(); } } <% end %>