Skip to content

Commit

Permalink
feat: add recipe foods controller to create and remove recipe food
Browse files Browse the repository at this point in the history
  • Loading branch information
demesameneshoa committed Dec 20, 2023
1 parent 2d07ef7 commit cfabbdf
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions app/controllers/recipe_foods_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class RecipeFoodsController < ApplicationController

def index
#match the recipe id with the recipe_foods recipe_id
@recipe_foods = RecipeFood.where(recipe_id: params[:recipe_id])
@recipe = Recipe.find(params[:recipe_id])
@food = Food.where(food_id: params[:food_id])
end

def new
@recipe_food = RecipeFood.new
@recipe_id = params[:recipe_id]
@foods = Food.all
end

def create
@recipe_food = RecipeFood.new(recipe_food_params)
if @recipe_food.save
flash[:success] = 'Ingredient was successfully added.'
redirect_to recipe_path(id: @recipe_food.recipe_id)
else
render :new, alert: 'Failed to add ingredient'
end
end

def update
respond_to do |format|
if @recipe_food.update(recipe_food_params)
flash[:success] = 'Recipe was successfully updated.'
format.html { redirect_to recipe_food_path(@recipe_food) }
else
flash[:danger] = 'Recipe was not updated.'
format.html { render :edit, status: :unprocessable_entity }
end
end
end

def destroy
@recipe_id = params[:recipe_id]
@recipe_food = RecipeFood.find(params[:id])
return unless @recipe_food.destroy
flash[:success] = 'Ingredient was successfully deleted.'
redirect_to recipe_path(id: @recipe_food.recipe_id)
end
private

def recipe_food_params
params.require(:recipe_food).permit(:quantity, :recipe_id, :food_id)
end
end

0 comments on commit cfabbdf

Please sign in to comment.