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

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..93b77a25 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -1,27 +1,23 @@

- Movie #<%= @the_movie.id %> details + Movie #<%= @the_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 +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
diff --git a/config/routes.rb b/config/routes.rb index c5ce269c..b489b094 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,22 +1,8 @@ Rails.application.routes.draw do - get("/", { :controller => "movies", :action => "index" }) + root "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