Show announcement validation errors on the frontend

This commit is contained in:
Karina Kwiatek 2020-04-19 21:26:55 +01:00
parent 2ecc746e23
commit 473f2cdcc5
3 changed files with 23 additions and 1 deletions

View File

@ -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

View File

@ -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"

View File

@ -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"