1
0

Added Ingredient#destroy

This commit is contained in:
cryptogopher 2019-09-15 19:00:04 +02:00
parent 893e2646d0
commit 9878b42655
2 changed files with 16 additions and 1 deletions

View File

@ -1,5 +1,6 @@
class IngredientsController < ApplicationController
before_action :find_project_by_project_id, only: [:index, :create]
before_action :find_ingredient, only: [:destroy]
before_action :authorize
def index
@ -20,6 +21,11 @@ class IngredientsController < ApplicationController
end
def destroy
# FIXME: don't destroy if any meal depend on ingredient
if @ingredient.destroy
flash[:notice] = 'Deleted ingredient'
end
redirect_to project_ingredients_url(@project)
end
private
@ -40,4 +46,13 @@ class IngredientsController < ApplicationController
]
)
end
# :find_* methods are called before :authorize,
# @project is required for :authorize to succeed
def find_ingredient
@ingredient = Ingredient.find(params[:id])
@project = @ingredient.project
rescue ActiveRecord::RecordNotFound
render_404
end
end

View File

@ -6,7 +6,7 @@ class Ingredient < ActiveRecord::Base
belongs_to :project
belongs_to :ref_unit, class_name: 'Unit'
has_many :nutrients, inverse_of: :ingredient
has_many :nutrients, inverse_of: :ingredient, dependent: :destroy
accepts_nested_attributes_for :nutrients, allow_destroy: true, reject_if: proc { |attrs|
attrs['quantity_id'].blank? && attrs['amount'].blank?
}