From 473f2cdcc52d81328fa5fc94608ec01dde2417b9 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 19 Apr 2020 21:26:55 +0100 Subject: [PATCH] Show announcement validation errors on the frontend --- app/controllers/announcement_controller.rb | 4 +++- app/views/announcement/edit.html.haml | 10 ++++++++++ app/views/announcement/new.html.haml | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/controllers/announcement_controller.rb b/app/controllers/announcement_controller.rb index 7c4a9b0a..93083c1e 100644 --- a/app/controllers/announcement_controller.rb +++ b/app/controllers/announcement_controller.rb @@ -23,7 +23,9 @@ class AnnouncementController < ApplicationController end def update - if Announcement.update(params[:id], announcement_params) + @announcement = Announcement.find(params[:id]) + @announcement.update(announcement_params) + if @announcement.save flash[:success] = "Announcement updated successfully." redirect_to announcement_index_path else diff --git a/app/views/announcement/edit.html.haml b/app/views/announcement/edit.html.haml index 387ff707..ee7eb3ba 100644 --- a/app/views/announcement/edit.html.haml +++ b/app/views/announcement/edit.html.haml @@ -1,6 +1,16 @@ - provide(:title, generate_title("Edit announcement")) .container.j2-page = bootstrap_form_for(@announcement, url: {action: "update"}, method: "PATCH") do |f| + - if @announcement.errors.any? + .row + .col-md-12 + .alert.alert-danger + %strong + = pluralize(@announcement.errors.count, "error") + prohibited this announcement from being saved: + %ul + - @announcement.errors.full_messages.each do |err| + %li= err .row .col-md-12 = f.text_area :content, label: "Content" diff --git a/app/views/announcement/new.html.haml b/app/views/announcement/new.html.haml index d41b08f1..57ed3eaf 100644 --- a/app/views/announcement/new.html.haml +++ b/app/views/announcement/new.html.haml @@ -1,6 +1,16 @@ - provide(:title, generate_title("Add new announcement")) .container.j2-page = bootstrap_form_for(@announcement, url: {action: "create"}) do |f| + - if @announcement.errors.any? + .row + .col-md-12 + .alert.alert-danger + %strong + = pluralize(@announcement.errors.count, "error") + prohibited this announcement from being saved: + %ul + - @announcement.errors.full_messages.each do |err| + %li= err .row .col-md-12 = f.text_area :content, label: "Content"