generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add recipe foods controller to create and remove recipe food
- Loading branch information
1 parent
2d07ef7
commit cfabbdf
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |