From 20a64b9033d6025363af6564d90ce3725d06899d Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Wed, 27 Sep 2023 08:57:04 -0500 Subject: [PATCH] Complete Lesson 40 --- app/controllers/favorites_controller.rb | 5 ++++- app/controllers/movies_controller.rb | 10 ++++++---- app/controllers/reviews_controller.rb | 2 +- app/models/movie.rb | 12 ++++++++++++ db/migrate/20230927133300_add_slug_to_movies.rb | 5 +++++ db/schema.rb | 3 ++- 6 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20230927133300_add_slug_to_movies.rb diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index 40385a5..48d792b 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -1,7 +1,7 @@ class FavoritesController < ApplicationController before_action :require_signin + before_action :set_movie def create - @movie = Movie.find(params[:movie_id]) @movie.favorites.create!(user: current_user) redirect_to @movie end @@ -11,4 +11,7 @@ class FavoritesController < ApplicationController redirect_to Movie.find(params[:movie_id]) end + def set_movie + @movie = Movie.find_by!(slug: params[:movie_id]) + end end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 27c2427..a010e6b 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,5 +1,7 @@ class MoviesController < ApplicationController + before_action :require_signin, except: [:index, :show] before_action :require_admin, except: [:index, :show] + before_action :set_movie, only: [:show, :edit, :update, :destroy] def index case params[:filter] @@ -12,7 +14,6 @@ class MoviesController < ApplicationController end end def show - @movie = Movie.find(params[:id]) @fans = @movie.fans @genres = @movie.genres.order(:name) @@ -21,10 +22,8 @@ class MoviesController < ApplicationController end end def edit - @movie = Movie.find(params[:id]) end def update - @movie = Movie.find(params[:id]) if @movie.update(movie_params) redirect_to @movie, notice: "Movie successfully updated!" else @@ -43,7 +42,6 @@ class MoviesController < ApplicationController end end def destroy - @movie = Movie.find(params[:id]) if @movie.destroy redirect_to movies_url, status: :see_other, alert: "Movie successfully destroyed!" end @@ -51,6 +49,10 @@ class MoviesController < ApplicationController private + def set_movie + @movie = Movie.find_by!(slug: params[:id]) + end + def movie_params params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross, :director, :duration, :image_file_name, genre_ids: []) end diff --git a/app/controllers/reviews_controller.rb b/app/controllers/reviews_controller.rb index 8be1a67..35f4073 100644 --- a/app/controllers/reviews_controller.rb +++ b/app/controllers/reviews_controller.rb @@ -21,7 +21,7 @@ class ReviewsController < ApplicationController private def set_movie - @movie = Movie.find(params[:movie_id]) + @movie = Movie.find_by!(slug: params[:movie_id]) end def review_params diff --git a/app/models/movie.rb b/app/models/movie.rb index 98b258d..c1771fb 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,4 +1,6 @@ class Movie < ApplicationRecord + before_save :set_slug + validates :title, :released_on, :duration, presence: true validates :description, length: { minimum: 25 } validates :total_gross, numericality: { greater_than_or_equal_to: 0 } @@ -26,4 +28,14 @@ class Movie < ApplicationRecord def average_stars reviews.average(:stars) || 0.0 end + + def to_param + slug + end + + private + + def set_slug + self.slug = title.parameterize + end end diff --git a/db/migrate/20230927133300_add_slug_to_movies.rb b/db/migrate/20230927133300_add_slug_to_movies.rb new file mode 100644 index 0000000..394218d --- /dev/null +++ b/db/migrate/20230927133300_add_slug_to_movies.rb @@ -0,0 +1,5 @@ +class AddSlugToMovies < ActiveRecord::Migration[7.0] + def change + add_column :movies, :slug, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 67bb35b..cda7184 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_09_27_130526) do +ActiveRecord::Schema[7.0].define(version: 2023_09_27_133300) do create_table "characterizations", force: :cascade do |t| t.integer "movie_id", null: false t.integer "genre_id", null: false @@ -46,6 +46,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_27_130526) do t.string "director" t.string "duration" t.string "image_file_name" + t.string "slug" end create_table "reviews", force: :cascade do |t|