Skip to content

Commit

Permalink
create ingredients when form is submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Kirchhoff committed Aug 2, 2020
1 parent f83256a commit 4081634
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/controllers/api/v1/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ def create
instructions: JSON.parse(params[:instructions]),
user: current_user
)

recipe.image.attach(params[:image]) if params[:image].present?

JSON.parse(params[:ingredients]).each do |ingredient_data|
ingredient = Ingredient.find_or_create_by!(name: ingredient_data['name'].downcase.strip)
recipe.ingredient_quantities << IngredientQuantity.new(
ingredient: ingredient,
quantity: ingredient_data['quantity'],
unit: ingredient_data['unit'].downcase.strip
)
end

recipe.save!
render json: nil, status: :created
rescue ActiveRecord::RecordInvalid => e
Expand Down

0 comments on commit 4081634

Please sign in to comment.