diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 51ecddc..27f4f06 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -9,8 +9,35 @@ class IngredientsController < ApplicationController end def create + @ingredient = Ingredient.new(ingredient_params.update(project: @project)) + if @ingredient.save + flash[:notice] = 'Created new ingredient' + redirect_to project_ingredients_url(@project) + else + @ingredients = @project.ingredients + render :index + end end def destroy end + + private + + def ingredient_params + params.require(:ingredient).permit( + :name, + :ref_amount, + :ref_unit_id, + :group, + nutrients_attributes: + [ + :id, + :quantity_id, + :amount, + :unit_id, + :_destroy + ] + ) + end end diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 3e41095..75cc9c2 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -4,8 +4,12 @@ class Ingredient < ActiveRecord::Base } belongs_to :project - has_many :nutrients + belongs_to :ref_unit, class_name: 'Unit' + has_many :nutrients, inverse_of: :ingredient accepts_nested_attributes_for :nutrients, allow_destroy: true + #reject_if: proc { |attrs| + # attrs['quantity_id'].blank? && attrs['amount'].blank? + #} validates :project, associated: true validates :name, presence: true, uniqueness: {scope: :project_id} diff --git a/app/models/nutrient.rb b/app/models/nutrient.rb index c56c570..d445c17 100644 --- a/app/models/nutrient.rb +++ b/app/models/nutrient.rb @@ -1,8 +1,10 @@ class Nutrient < ActiveRecord::Base - belongs_to :ingredient + belongs_to :ingredient, inverse_of: :nutrients + belongs_to :quantity + belongs_to :unit validates :ingredient, presence: true, associated: true - validates :quantity, presence: true, associated: true + validates :quantity, presence: true, associated: true, uniqueness: {scope: :ingredient_id} validates :amount, numericality: {greater_than: 0} validates :unit, presence: true, associated: true end diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb index 83695d3..7d861c5 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/ingredients/_form.html.erb @@ -8,11 +8,11 @@
<%= f.select :group, group_options, required: true %>
<% @ingredient.nutrients.each_with_index do |n, index| %> - <%= f.fields_for :nutrient, n, index: '' do |ff| %> + <%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %><%= ff.select :quantity_id, quantity_options, label: (index > 0 ? '' : :field_nutrients) %> - <%= ff.number_field :amount, {size: 8, label: ''} %> + <%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %> <%= ff.select :unit_id, unit_options, label: '' %> <%= ff.check_box :_destroy, {style: "display:none", label: ''} %> <%= link_to t(".button_delete_nutrient"), '#', diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index b31b83e..d7d0ba1 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -29,7 +29,6 @@