Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Zarazan committed Nov 26, 2024
1 parent 78cc4b8 commit bd06def
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/store/recipesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions app/models/food.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Food < ApplicationRecord
has_many :ingredients

validates :name, presence: true, uniqueness: true
end
2 changes: 2 additions & 0 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bd06def

Please sign in to comment.