From 1656289d7d07dd32d5f54654b50ecba812b597d3 Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Tue, 26 Sep 2023 11:48:35 -0500 Subject: [PATCH] Complete Lesson 19 --- app/controllers/movies_controller.rb | 14 ++++++++++---- app/views/movies/_form.html.erb | 2 ++ app/views/shared/_errors.html.erb | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 app/views/shared/_errors.html.erb diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 575df53..3f11d4c 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -10,16 +10,22 @@ class MoviesController < ApplicationController end def update @movie = Movie.find(params[:id]) - @movie.update(movie_params) - redirect_to @movie + if @movie.update(movie_params) + redirect_to @movie + else + render :edit, status: :unprocessable_entity + end end def new @movie = Movie.new end def create @movie = Movie.new(movie_params) - @movie.save - redirect_to @movie + if @movie.save + redirect_to @movie + else + render :new, status: :unprocessable_entity + end end def destroy @movie = Movie.find(params[:id]) diff --git a/app/views/movies/_form.html.erb b/app/views/movies/_form.html.erb index 0a510ff..3a31a44 100644 --- a/app/views/movies/_form.html.erb +++ b/app/views/movies/_form.html.erb @@ -1,4 +1,6 @@ <%= form_with(model: movie) do |f| %> + <%= render "shared/errors", object: movie %> + <%= f.label :title %> <%= f.text_field :title, autofocus: true %> diff --git a/app/views/shared/_errors.html.erb b/app/views/shared/_errors.html.erb new file mode 100644 index 0000000..3eb1a9a --- /dev/null +++ b/app/views/shared/_errors.html.erb @@ -0,0 +1,16 @@ +<% if object.errors.any? %> +
+

+ Oops! Your form could not be saved. +

+

+ Please correct the following + <%= pluralize(object.errors.size, "error") %>: +

+ +
+<% end %>