diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 1cb00fd..36e5b30 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -38,7 +38,8 @@ def recipe_params rp[:ingredients_attributes].each do |ingredient| food_name = ingredient.delete(:food_name) if ingredient[:food_id].blank? || ingredient[:food_id] == 0 - ingredient[:food_id] = Food.find_or_create_by!(name: food_name).id + new_food = Food.new(name: food_name) + ingredient[:food_id] = new_food.id if new_food.save end end rp diff --git a/app/javascript/store/recipesSlice.ts b/app/javascript/store/recipesSlice.ts index ba535f0..812c9da 100644 --- a/app/javascript/store/recipesSlice.ts +++ b/app/javascript/store/recipesSlice.ts @@ -24,7 +24,7 @@ export const createRecipe = createAsyncThunk( if (!response.ok) { const errorData = await response.json(); - throw new Error(errorData.error || 'An error occurred'); + throw new Error(errorData.errors || 'Unknown error'); } return response.json(); diff --git a/app/models/food.rb b/app/models/food.rb index acd99a8..b342b6c 100644 --- a/app/models/food.rb +++ b/app/models/food.rb @@ -1,3 +1,5 @@ class Food < ApplicationRecord has_many :ingredients + + validates :name, presence: true, uniqueness: true end diff --git a/app/models/recipe.rb b/app/models/recipe.rb index ea8a096..cd2c5a3 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -3,6 +3,8 @@ class Recipe < ApplicationRecord accepts_nested_attributes_for :ingredients, allow_destroy: true + validates :name, presence: true + after_save :broadcast_recipe def to_json