diff --git a/immersive/rails/makerbistro/app/controllers/ingredients_controller.rb b/immersive/rails/makerbistro/app/controllers/ingredients_controller.rb index cf74cd4..d065ca0 100644 --- a/immersive/rails/makerbistro/app/controllers/ingredients_controller.rb +++ b/immersive/rails/makerbistro/app/controllers/ingredients_controller.rb @@ -1,9 +1,11 @@ class IngredientsController < ApplicationController before_action :set_menu_item, only: [:create] - before_action :get_new_ingredient, only: [:create] def create - @ingredient = @menu_item.ingredients.new(@new_ingredient) + @ingredient = @menu_item.ingredients.where(:name => ingredient_params[:name]).first_or_create do |ingredient| + @menu_item.ingredients << ingredient + end + if @menu_item.save redirect_to @menu_item, notice: "The ingredient was successfully created!" else @@ -13,11 +15,11 @@ def create private - def set_menu_item - @menu_item = MenuItem.find(params[:menu_item_id]) - end + def set_menu_item + @menu_item = MenuItem.find(params[:menu_item_id]) + end - def get_new_ingredient - @new_ingredient = params.require(:ingredient).permit(:name) - end + def ingredient_params + params.require(:ingredient).permit(:name) + end end