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