2016-01-12 18:43:00 -06:00
|
|
|
class RecipesController < ApplicationController
|
2016-01-18 19:41:26 -06:00
|
|
|
before_action :set_recipe, only: [:show, :edit, :update, :destroy, :scale]
|
2016-01-12 18:43:00 -06:00
|
|
|
|
|
|
|
# GET /recipes
|
|
|
|
# GET /recipes.json
|
|
|
|
def index
|
2016-01-18 19:41:26 -06:00
|
|
|
@recipes = Recipe.active
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /recipes/1
|
|
|
|
# GET /recipes/1.json
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
2016-01-18 19:41:26 -06:00
|
|
|
# GET /recipes/1
|
|
|
|
def scale
|
|
|
|
@scale = params[:factor]
|
|
|
|
@recipe.scale(@scale)
|
|
|
|
render :show
|
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
# GET /recipes/new
|
|
|
|
def new
|
|
|
|
@recipe = Recipe.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /recipes/1/edit
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /recipes
|
|
|
|
# POST /recipes.json
|
|
|
|
def create
|
|
|
|
@recipe = Recipe.new(recipe_params)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @recipe.save
|
|
|
|
format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' }
|
|
|
|
format.json { render :show, status: :created, location: @recipe }
|
|
|
|
else
|
|
|
|
format.html { render :new }
|
|
|
|
format.json { render json: @recipe.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PATCH/PUT /recipes/1
|
|
|
|
# PATCH/PUT /recipes/1.json
|
|
|
|
def update
|
|
|
|
respond_to do |format|
|
|
|
|
if @recipe.update(recipe_params)
|
|
|
|
format.html { redirect_to @recipe, notice: 'Recipe was successfully updated.' }
|
|
|
|
format.json { render :show, status: :ok, location: @recipe }
|
|
|
|
else
|
|
|
|
format.html { render :edit }
|
|
|
|
format.json { render json: @recipe.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /recipes/1
|
|
|
|
# DELETE /recipes/1.json
|
|
|
|
def destroy
|
2016-01-18 19:41:26 -06:00
|
|
|
@recipe.deleted = true
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
respond_to do |format|
|
2016-01-18 19:41:26 -06:00
|
|
|
if @recipe.save
|
|
|
|
format.html { redirect_to recipes_url, notice: 'Recipe was successfully destroyed.' }
|
|
|
|
format.json { head :no_content }
|
|
|
|
else
|
|
|
|
format.html { redirect_to recipes_url, error: 'Recipe could not be destroyed.' }
|
|
|
|
format.json { head :no_content }
|
|
|
|
end
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
def set_recipe
|
|
|
|
@recipe = Recipe.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
# Never trust parameters from the scary internet, only allow the white list through.
|
|
|
|
def recipe_params
|
2016-01-13 17:10:43 -06:00
|
|
|
params.require(:recipe).permit(:name, :description, :source, :yields, :total_time, :active_time, recipe_ingredients_attributes: [:custom_name, :custom_density, :ingredient_id, :quantity, :units, :sort_order, :id, :_destroy], recipe_steps_attributes: [:step, :sort_order, :id, :_destroy])
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
|
|
|
end
|