diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 93a06ad0..3aa43772 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,80 +1,54 @@ class MoviesController < ApplicationController def new - @the_movie = Movie.new - - render template: "movies/new.html.erb" + @movie = Movie.new end def index - matching_movies = Movie.all - - @list_of_movies = matching_movies.order({ :created_at => :desc }) + @movies = Movie.order(created_at: :desc) respond_to do |format| - format.json do - render json: @list_of_movies - end + format.json { render json: @movies } - format.html do - render({ :template => "movies/index.html.erb" }) - end + format.html end end def show - the_id = params.fetch(:id) - - matching_movies = Movie.where({ :id => the_id }) - - @the_movie = matching_movies.first - - render({ :template => "movies/show.html.erb" }) + @movie = Movie.find(params.fetch(:id)) end def create - @the_movie = Movie.new - @the_movie.title = params.fetch("query_title") - @the_movie.description = params.fetch("query_description") + movie_params = params.require(:movie).permit(:title, :description) + + @movie = Movie.new(movie_params) if @the_movie.valid? @the_movie.save - redirect_to("/movies", { :notice => "Movie created successfully." }) + redirect_to movies_url, notice: "Created movie." else - render template: "movies/new.html.erb" + render "new" end end def edit - the_id = params.fetch(:id) - - matching_movies = Movie.where({ :id => the_id }) - - @the_movie = matching_movies.first - - render({ :template => "movies/edit.html.erb" }) + @movie = Movie.find(params.fetch(:id)) end def update - the_id = params.fetch(:id) - the_movie = Movie.where({ :id => the_id }).first + @movie = Movie.find(params.fetch(:id)) + movie_params = params.require(:movie).permit(:title, :description) - the_movie.title = params.fetch("query_title") - the_movie.description = params.fetch("query_description") - - if the_movie.valid? - the_movie.save - redirect_to("/movies/#{the_movie.id}", { :notice => "Movie updated successfully."} ) + if @movie.update(movie_params) + redirect_to @movie, notice: "Updated movie." else - redirect_to("/movies/#{the_movie.id}", { :alert => "Movie failed to update successfully." }) + render "edit" end end def destroy - the_id = params.fetch(:id) - the_movie = Movie.where({ :id => the_id }).first - - the_movie.destroy - - redirect_to("/movies", { :notice => "Movie deleted successfully."} ) + @movie = Movie.find(params.fetch(:id)) + @movie.destroy + + redirect_to movies_url, notice: "Deleted movie." end end diff --git a/app/views/movies/edit.html.erb b/app/views/movies/edit.html.erb index ce212ba3..93de1705 100644 --- a/app/views/movies/edit.html.erb +++ b/app/views/movies/edit.html.erb @@ -1,31 +1,23 @@ -
<%= message %>
<% end %> - +<% end %> diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb index 8177f145..ef88039a 100644 --- a/app/views/movies/index.html.erb +++ b/app/views/movies/index.html.erb @@ -5,7 +5,7 @@<%= message %>
<% end %> - +<% end %> diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb index 5609487e..93b77a25 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -1,27 +1,23 @@