From 717fa996842012a8666bcff985218c2abddd9991 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Thu, 16 Mar 2023 22:36:37 -0600 Subject: Mejora ProductsController --- app/controllers/api/products_controller.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 2822a21..ec82af6 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -6,7 +6,7 @@ module Api def show @product = Product.find_by(public_id: params[:id]) - render json: { error_message: 'No existe el producto' }, status: :not_found and return if @product.nil? + render json: not_found_error_message, status: :not_found and return if @product.nil? render json: serialized_object.serializable_hash, status: :ok end @@ -23,7 +23,7 @@ module Api if @product.save render json: serialized_object.serializable_hash, status: :ok else - render json: @product.errors.full_messages, status: :unprocessable_entity + render json: { error_messages: @product.errors.full_messages }, status: :unprocessable_entity end end @@ -33,14 +33,14 @@ module Api if @product.update(object_params) render json: serialized_object.serializable_hash, status: :ok else - render json: @product.errors.full_messages, status: :unprocessable_entity + render json: { error_messages: @product.errors.full_messages }, status: :unprocessable_entity end end def destroy @product = Product.find_by(public_id: params[:id]) - render json: { error_message: 'No existe el producto' }, status: :not_found and return if @product.nil? + render json: not_found_error_message, status: :not_found and return if @product.nil? @product.destroy render status: :see_other @@ -71,5 +71,9 @@ module Api categories = permitted_params[:categories].split(',') permitted_params.merge(categories:, public_id: SecureRandom.hex(12)) end + + def not_found_error_message + { error_message: 'No existe el producto' } + end end end -- cgit v1.2.3