From 6eea26374c6491376df6502c6acee324e7bbcc0a Mon Sep 17 00:00:00 2001 From: Ankur Date: Tue, 22 Nov 2022 15:01:24 +0530 Subject: [PATCH] updated flash errors with correct error messages --- app/controllers/letters_controller.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/controllers/letters_controller.rb b/app/controllers/letters_controller.rb index 00dc115..d6fdb62 100644 --- a/app/controllers/letters_controller.rb +++ b/app/controllers/letters_controller.rb @@ -9,7 +9,14 @@ def create flash[:notice] = t('flash.saved_letter_successfully') redirect_to letter_path(@letter) else - flash[:error] = 'Letter description cannot be empty. Try Again!' + # flash[:error] = 'Letter description cannot be empty. Try Again!' + if @letter.errors.many? + flash[:error] = "There are #{view_context.pluralize(@letter.errors.count,"error")}. + #{@letter.errors.full_messages.join("\n")}" + else + flash[:error] = "There is #{view_context.pluralize(@letter.errors.count,"error")}. + #{@letter.errors.full_messages.join("\n")}" + end redirect_to root_path end end @@ -29,7 +36,14 @@ def update if @letter.update(letter_params) flash[:notice] = t('flash.updated_letter_successfully') else - flash[:error] = 'Letter description cannot be empty. Try Again!' + # flash[:error] = 'Letter description cannot be empty. Try Again!' + if @letter.errors.many? + flash[:error] = "There are #{view_context.pluralize(@letter.errors.count,"error")}. + #{@letter.errors.full_messages.join("\n")}" + else + flash[:error] = "There is #{view_context.pluralize(@letter.errors.count,"error")}. + #{@letter.errors.full_messages.join("\n")}" + end end redirect_to letter_path(@letter) end