Adding/deleting nutrients on ingredient form working
This commit is contained in:
parent
dd8c4f2cd1
commit
faef248449
@ -4,7 +4,7 @@ class IngredientsController < ApplicationController
|
|||||||
|
|
||||||
def index
|
def index
|
||||||
@ingredient = Ingredient.new
|
@ingredient = Ingredient.new
|
||||||
@nutrient = Nutrient.new
|
@ingredient.nutrients.build
|
||||||
@ingredients = @project.ingredients
|
@ingredients = @project.ingredients
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ class Ingredient < ActiveRecord::Base
|
|||||||
|
|
||||||
belongs_to :project
|
belongs_to :project
|
||||||
has_many :nutrients
|
has_many :nutrients
|
||||||
accepts_nested_attributes_for :nutrients
|
accepts_nested_attributes_for :nutrients, allow_destroy: true
|
||||||
|
|
||||||
validates :project, associated: true
|
validates :project, associated: true
|
||||||
validates :name, presence: true, uniqueness: {scope: :project_id}
|
validates :name, presence: true, uniqueness: {scope: :project_id}
|
||||||
|
@ -7,34 +7,44 @@
|
|||||||
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
|
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
|
||||||
</p>
|
</p>
|
||||||
<p><%= f.select :group, group_options, required: true %></p>
|
<p><%= f.select :group, group_options, required: true %></p>
|
||||||
<%= f.fields_for :nutrients, @nutrient do |ff| %>
|
<% @ingredient.nutrients.each_with_index do |n, index| %>
|
||||||
<p class="nutrient" data-id="<%= ff.index %>">
|
<%= f.fields_for :nutrient, n, index: '' do |ff| %>
|
||||||
|
<p class="nutrient">
|
||||||
<%= ff.select :quantity_id, quantity_options,
|
<%= ff.select :quantity_id, quantity_options,
|
||||||
label: (ff.index > 0 ? '' : :field_nutrients) %>
|
label: (index > 0 ? '' : :field_nutrients) %>
|
||||||
<%= ff.number_field :amount, {size: 8, label: ''} %>
|
<%= ff.number_field :amount, {size: 8, label: ''} %>
|
||||||
<%= ff.select :unit_id, unit_options, label: '' %>
|
<%= ff.select :unit_id, unit_options, label: '' %>
|
||||||
|
<%= ff.check_box :_destroy, {style: "display:none", label: ''} %>
|
||||||
<%= link_to t(".button_delete_nutrient"), '#',
|
<%= link_to t(".button_delete_nutrient"), '#',
|
||||||
:class => 'icon icon-del',
|
:class => 'icon icon-del',
|
||||||
:style => "display:none",
|
:style => "display:none",
|
||||||
:onclick => "deleteNutrient("+ff.index.to_s+"); return false;" %>
|
:onclick => "deleteNutrient(); return false;" %>
|
||||||
</p>
|
</p>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
<% end %>
|
||||||
<p><%= link_to t(".button_add_nutrient"), '#', :class => 'icon icon-add',
|
<p><%= link_to t(".button_add_nutrient"), '#', :class => 'icon icon-add',
|
||||||
:onclick => 'addNutrient(); return false;' %></p>
|
:onclick => 'addNutrient(); return false;' %></p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= javascript_tag do %>
|
<%= javascript_tag do %>
|
||||||
function addNutrient() {
|
function addNutrient() {
|
||||||
var row = $('#ingredient-form p.nutrient:last');
|
var row = $('p.nutrient:visible:last');
|
||||||
var id = parseInt(row.attr('data-id')) + 1;
|
|
||||||
var new_row = row.clone().insertAfter(row);
|
var new_row = row.clone().insertAfter(row);
|
||||||
new_row.attr('data-id', id)
|
new_row.find('input[id$=_amount], select[id$=_quantity_id]').val('');
|
||||||
new_row.find('input, select').val('');
|
new_row.find('select[id$=_unit_id]').val(row.find('select[id$=_unit_id]').val());
|
||||||
new_row.find('label').text('');
|
new_row.find('label:first').text('');
|
||||||
if ($('#ingredient-form p.nutrient').length > 1) {
|
if ($('p.nutrient:visible').length > 1) {
|
||||||
$('#ingredient-form p.nutrient a.icon-del').show();
|
$('p.nutrient a.icon-del').show();
|
||||||
} else {
|
}
|
||||||
$('#ingredient-form p.nutrient.icon-del').hide();
|
}
|
||||||
|
|
||||||
|
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 %>
|
<% end %>
|
||||||
|
Reference in New Issue
Block a user