From b8bb18291cbefcfe0bbab86e7b865dc8e8f313ec Mon Sep 17 00:00:00 2001 From: YungKeeks Date: Wed, 1 Feb 2023 04:12:38 +0000 Subject: [PATCH 1/4] Updated movies controller --- app/controllers/movies_controller.rb | 66 +++++++++------------------- config/routes.rb | 19 ++------ 2 files changed, 23 insertions(+), 62 deletions(-) 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/config/routes.rb b/config/routes.rb index c5ce269c..18a83934 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,22 +1,9 @@ Rails.application.routes.draw do - get("/", { :controller => "movies", :action => "index" }) + get "movies#index" # Routes for the Movie resource: - # CREATE - post("/movies", { :controller => "movies", :action => "create" }) - get("/movies/new", { :controller => "movies", :action => "new" }) - - # READ - get("/movies", { :controller => "movies", :action => "index" }) - get("/movies/:id", { :controller => "movies", :action => "show" }) - - # UPDATE - patch("/movies/:id", { :controller => "movies", :action => "update" }) - get("/movies/:id/edit", { :controller => "movies", :action => "edit" }) - - # DELETE - delete("/movies/:id", { :controller => "movies", :action => "destroy" }) + resources :movies #------------------------------ -end \ No newline at end of file +end From c60c822f74e97ac9b4e2a9a1ef79fae325563748 Mon Sep 17 00:00:00 2001 From: YungKeeks Date: Wed, 1 Feb 2023 04:13:01 +0000 Subject: [PATCH 2/4] Updated movies controller --- config/routes.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 18a83934..b489b094 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,8 @@ Rails.application.routes.draw do - get "movies#index" + root "movies#index" # Routes for the Movie resource: resources :movies - #------------------------------ end From 3a9295643fde511d0cbd80c062d052bf64d4c2d9 Mon Sep 17 00:00:00 2001 From: YungKeeks Date: Wed, 1 Feb 2023 04:19:34 +0000 Subject: [PATCH 3/4] All but show movies --- app/views/movies/edit.html.erb | 30 +++++++++++------------------- app/views/movies/index.html.erb | 18 ++++++++---------- app/views/movies/new.html.erb | 24 +++++++++--------------- app/views/movies/show.html.erb | 2 +- 4 files changed, 29 insertions(+), 45 deletions(-) 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 @@ -

Edit movie

+

Edit Movie

-<% @the_movie.errors.full_messages.each do |message| %> +<% @movie.errors.full_messages.each do |message| %>

<%= message %>

<% end %> -
- - - +<%= form_with model: @movie do |form| %>
- - - + <%= form.label :title %> + <%= form.text_field :title %>
- + <%= form.label :description %> + <%= form.text_field :description %> +
- +
+ <%= form.submit %>
- -
+<% 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 @@
- Add a new movie + <%= link_to "Add New Movie", new_movie_path %>

@@ -36,31 +36,29 @@ - <% @list_of_movies.each do |a_movie| %> + <% @movies.each do |a_movie| %> - <%= a_movie.id %> + <%= movie.id %> - <%= a_movie.title %> + <%= movie.title %> - <%= a_movie.description %> + <%= movie.description %> - <%= time_ago_in_words(a_movie.created_at) %> ago + <%= time_ago_in_words(movie.created_at) %> ago - <%= time_ago_in_words(a_movie.updated_at) %> ago + <%= time_ago_in_words(movie.updated_at) %> ago - - Show details - + <%= link_to 'Show Details', movie %> <% end %> diff --git a/app/views/movies/new.html.erb b/app/views/movies/new.html.erb index b4a501fe..4a7ddd69 100644 --- a/app/views/movies/new.html.erb +++ b/app/views/movies/new.html.erb @@ -4,26 +4,20 @@

<%= message %>

<% end %> -
- +<%= form_with model: @movie do |form| %>
- - - + <%= form.label :title %> + <%= form.text_field :title %>
- + <%= form.label :description %> + <%= form.text_field :description %> +
- +
+ <%= form.submit %>
- -
+<% end %> diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb index 5609487e..f352505c 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -1,7 +1,7 @@

- Movie #<%= @the_movie.id %> details + Movie #<%= @the_movie.id %> Details

From 0df2efbaac62c3d78ae1fc61961891f4286ad7a7 Mon Sep 17 00:00:00 2001 From: YungKeeks Date: Wed, 1 Feb 2023 04:26:12 +0000 Subject: [PATCH 4/4] All Completed --- app/views/movies/show.html.erb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb index f352505c..93b77a25 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -5,23 +5,19 @@
+
- - Go back - + <%= link_to "Go Back", movies_path %>
- - Edit Movie - + <%= link_to "Edit Movie", edit_movie_path(@movie) %>
- - Delete Movie - + <%= link_to "Delete Movie", @movie, method: :delete %>
+
@@ -29,28 +25,28 @@ Title
- <%= @the_movie.title %> + <%= @movie.title %>
Description
- <%= @the_movie.description %> + <%= @movie.description %>
Created at
- <%= time_ago_in_words(@the_movie.created_at) %> ago + <%= time_ago_in_words(@movie.created_at) %> ago
Updated at
- <%= time_ago_in_words(@the_movie.updated_at) %> ago + <%= time_ago_in_words(@movie.updated_at) %> ago