diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 08f0768..c0cb0c9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,14 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + def verified_request? + if request.content_type == "application/json" + true + else + super() + end + end + def ensure_valid_user unless current_user? flash[:warning] = "You must login" diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 1e32c6c..875db4b 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -6,7 +6,7 @@ class RecipesController < ApplicationController # GET /recipes def index - @criteria = ViewModels::RecipeCriteria.new(params[:criteria]) + @criteria = ViewModels::RecipeCriteria.new(criteria_params) @criteria.page = params[:page] @criteria.per = params[:per] @recipes = Recipe.for_criteria(@criteria).includes(:tags) @@ -40,26 +40,6 @@ class RecipesController < ApplicationController end end - @recipe = RecipeDecorator.decorate(@recipe, view_context) - - end - - # GET /recipes/1 - def scale - @scale = params[:factor] - @recipe.scale(@scale, true) - @recipe = RecipeDecorator.decorate(@recipe, view_context) - render :show - end - - # GET /recipes/new - def new - @recipe = Recipe.new - end - - # GET /recipes/1/edit - def edit - ensure_owner @recipe end # POST /recipes @@ -68,9 +48,9 @@ class RecipesController < ApplicationController @recipe.user = current_user if @recipe.save - redirect_to @recipe, notice: 'Recipe was successfully created.' + render json: { success: true } else - render :new + render json: @recipe.errors, status: :unprocessable_entity end end @@ -78,13 +58,20 @@ class RecipesController < ApplicationController def update ensure_owner(@recipe) do if @recipe.update(recipe_params) - redirect_to @recipe, notice: 'Recipe was successfully updated.' + render json: { success: true } else - render :edit + puts '====' + puts @recipe.recipe_ingredients.map { |ri| ri.ingredient_id.class }.join("|") + render json: @recipe.errors, status: :unprocessable_entity end end end + # POST /recipes/preview_steps + def preview_steps + render json: { rendered_steps: MarkdownProcessor.render(params[:step_text]) } + end + # DELETE /recipes/1 def destroy ensure_owner(@recipe) do @@ -108,4 +95,8 @@ class RecipesController < ApplicationController def recipe_params params.require(:recipe).permit(:name, :description, :source, :yields, :total_time, :active_time, :step_text, tag_names: [], recipe_ingredients_attributes: [:name, :ingredient_id, :quantity, :units, :preparation, :sort_order, :id, :_destroy]) end + + def criteria_params + params.require(:criteria).permit(*ViewModels::RecipeCriteria::PARAMS) + end end diff --git a/app/javascript/components/App.vue b/app/javascript/components/App.vue index cd4d8b0..5fbebd1 100644 --- a/app/javascript/components/App.vue +++ b/app/javascript/components/App.vue @@ -1,7 +1,8 @@