diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 93a06ad..13820e3 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,80 +1,57 @@ 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." }) + if @movie.valid? + @movie.save + + redirect_to movies_url, notice: "Movie created successfully." 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 - - the_movie.title = params.fetch("query_title") - the_movie.description = params.fetch("query_description") + @movie = Movie.find(params.fetch(:id)) - if the_movie.valid? - the_movie.save - redirect_to("/movies/#{the_movie.id}", { :notice => "Movie updated successfully."} ) + movie_params = params.require(:movie).permit(:title, :description) + + if @movie.update(movie_params) + redirect_to @movie, notice: "Movie updated successfully." 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 + @movie = Movie.find(params.fetch(:id)) - the_movie.destroy + @movie.destroy - redirect_to("/movies", { :notice => "Movie deleted successfully."} ) + redirect_to movies_url, notice: "Movie deleted successfully." end end diff --git a/app/views/movies/edit.html.erb b/app/views/movies/edit.html.erb index f6a18d0..afaf36d 100644 --- a/app/views/movies/edit.html.erb +++ b/app/views/movies/edit.html.erb @@ -1,31 +1,21 @@

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_area :description %>
- -
+
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb index 8177f14..1e3e155 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 a new movie", new_movie_path %>

@@ -36,31 +36,29 @@ - <% @list_of_movies.each do |a_movie| %> + <% @movies.each do |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 8ca875b..267786c 100644 --- a/app/views/movies/new.html.erb +++ b/app/views/movies/new.html.erb @@ -1,29 +1,21 @@

New 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_area :description %>
- -
+
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb index 92487a0..730aecf 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -1,26 +1,20 @@

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

- - 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 +23,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
diff --git a/config/routes.rb b/config/routes.rb index c5ce269..664b650 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,22 +1,5 @@ Rails.application.routes.draw do - get("/", { :controller => "movies", :action => "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" }) + root "movies#index" - # UPDATE - patch("/movies/:id", { :controller => "movies", :action => "update" }) - get("/movies/:id/edit", { :controller => "movies", :action => "edit" }) - - # DELETE - delete("/movies/:id", { :controller => "movies", :action => "destroy" }) - - #------------------------------ -end \ No newline at end of file + resources :movies +end diff --git a/test/system/example_specs.rb b/test/system/movie_test.rb similarity index 93% rename from test/system/example_specs.rb rename to test/system/movie_test.rb index 2372c3c..116ce72 100644 --- a/test/system/example_specs.rb +++ b/test/system/movie_test.rb @@ -14,7 +14,7 @@ class MoviesTest < ApplicationSystemTestCase fill_in "Description", with: "A new movie's descroption" fill_in "Title", with: "A new movie's title" - click_on "Create movie" + click_on "Create Movie" assert_text "Movie created successfully" @@ -30,7 +30,7 @@ class MoviesTest < ApplicationSystemTestCase fill_in "Description", with: "Some other description" fill_in "Title", with: "Some other title" - click_on "Update movie" + click_on "Update Movie" assert_text "Movie updated successfully" @@ -45,7 +45,7 @@ class MoviesTest < ApplicationSystemTestCase visit "/movies" click_on "Show details", match: :first - click_on "Delete movie" + click_on "Delete Movie" assert_text "Movie deleted successfully"