Complete Lesson 19
This commit is contained in:
parent
db77371820
commit
1656289d7d
@ -10,16 +10,22 @@ class MoviesController < ApplicationController
|
|||||||
end
|
end
|
||||||
def update
|
def update
|
||||||
@movie = Movie.find(params[:id])
|
@movie = Movie.find(params[:id])
|
||||||
@movie.update(movie_params)
|
if @movie.update(movie_params)
|
||||||
redirect_to @movie
|
redirect_to @movie
|
||||||
|
else
|
||||||
|
render :edit, status: :unprocessable_entity
|
||||||
|
end
|
||||||
end
|
end
|
||||||
def new
|
def new
|
||||||
@movie = Movie.new
|
@movie = Movie.new
|
||||||
end
|
end
|
||||||
def create
|
def create
|
||||||
@movie = Movie.new(movie_params)
|
@movie = Movie.new(movie_params)
|
||||||
@movie.save
|
if @movie.save
|
||||||
redirect_to @movie
|
redirect_to @movie
|
||||||
|
else
|
||||||
|
render :new, status: :unprocessable_entity
|
||||||
|
end
|
||||||
end
|
end
|
||||||
def destroy
|
def destroy
|
||||||
@movie = Movie.find(params[:id])
|
@movie = Movie.find(params[:id])
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<%= form_with(model: movie) do |f| %>
|
<%= form_with(model: movie) do |f| %>
|
||||||
|
<%= render "shared/errors", object: movie %>
|
||||||
|
|
||||||
<%= f.label :title %>
|
<%= f.label :title %>
|
||||||
<%= f.text_field :title, autofocus: true %>
|
<%= f.text_field :title, autofocus: true %>
|
||||||
|
|
||||||
|
16
app/views/shared/_errors.html.erb
Normal file
16
app/views/shared/_errors.html.erb
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<% if object.errors.any? %>
|
||||||
|
<section class="errors">
|
||||||
|
<h2>
|
||||||
|
Oops! Your form could not be saved.
|
||||||
|
</h2>
|
||||||
|
<h3>
|
||||||
|
Please correct the following
|
||||||
|
<%= pluralize(object.errors.size, "error") %>:
|
||||||
|
</h3>
|
||||||
|
<ul>
|
||||||
|
<% object.errors.full_messages.each do |message| %>
|
||||||
|
<li><%= message %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
<% end %>
|
Loading…
x
Reference in New Issue
Block a user