<%= error_messages_for @food %>

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

<%= f.text_area :notes, cols: 40, rows: 3, required: false, style: "width: 100%;" %>

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

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

<%= f.select :source_id, source_options, {required: false, include_blank: t('.null_source')} %>

<%= f.text_field :source_ident, size: 25, required: false %>

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

<%= ff.hidden_field :id %> <%= ff.select :quantity_id, quantity_options, {include_blank: true, required: true, label: (index > 0 ? '' : :field_nutrients)} %> <%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %> <%= ff.select :unit_id, unit_options, {label: ''} %> <%= ff.hidden_field :_destroy %> <%= link_to t(".button_delete_nutrient"), '#', class: 'icon icon-del', style: (@food.nutrients.length > 1 ? "" : "display:none"), onclick: "deleteNutrient(); return false;" %>

<% end %> <% end %>

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

<%= javascript_tag do %> function newNutrient() { var form = $(event.target).closest('form'); var row = form.find('p.nutrient:visible:last'); var new_row = row.clone().insertAfter(row); new_row.find('input[id$=__id], 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('input[id$=__destroy]').val(''); new_row.find('label:first').hide(); form.find('p.nutrient:visible a.icon-del').show(); } function deleteNutrient() { var form = $(event.target).closest('form'); var row = $(event.target).closest('p.nutrient'); if (row.find('input[id$=__id]').val()) { row.hide(); row.find('input[id$=__destroy]').val('1'); } else { row.remove(); } form.find('p.nutrient:visible:first label:first').show(); if (form.find('p.nutrient:visible').length <= 1) { form.find('p.nutrient:visible a.icon-del').hide(); } } <% end %>