Complete Lesson 40
This commit is contained in:
parent
9fe5f43726
commit
20a64b9033
@ -1,7 +1,7 @@
|
|||||||
class FavoritesController < ApplicationController
|
class FavoritesController < ApplicationController
|
||||||
before_action :require_signin
|
before_action :require_signin
|
||||||
|
before_action :set_movie
|
||||||
def create
|
def create
|
||||||
@movie = Movie.find(params[:movie_id])
|
|
||||||
@movie.favorites.create!(user: current_user)
|
@movie.favorites.create!(user: current_user)
|
||||||
redirect_to @movie
|
redirect_to @movie
|
||||||
end
|
end
|
||||||
@ -11,4 +11,7 @@ class FavoritesController < ApplicationController
|
|||||||
|
|
||||||
redirect_to Movie.find(params[:movie_id])
|
redirect_to Movie.find(params[:movie_id])
|
||||||
end
|
end
|
||||||
|
def set_movie
|
||||||
|
@movie = Movie.find_by!(slug: params[:movie_id])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
class MoviesController < ApplicationController
|
class MoviesController < ApplicationController
|
||||||
|
before_action :require_signin, except: [:index, :show]
|
||||||
before_action :require_admin, except: [:index, :show]
|
before_action :require_admin, except: [:index, :show]
|
||||||
|
before_action :set_movie, only: [:show, :edit, :update, :destroy]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
case params[:filter]
|
case params[:filter]
|
||||||
@ -12,7 +14,6 @@ class MoviesController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
def show
|
def show
|
||||||
@movie = Movie.find(params[:id])
|
|
||||||
@fans = @movie.fans
|
@fans = @movie.fans
|
||||||
@genres = @movie.genres.order(:name)
|
@genres = @movie.genres.order(:name)
|
||||||
|
|
||||||
@ -21,10 +22,8 @@ class MoviesController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
def edit
|
def edit
|
||||||
@movie = Movie.find(params[:id])
|
|
||||||
end
|
end
|
||||||
def update
|
def update
|
||||||
@movie = Movie.find(params[:id])
|
|
||||||
if @movie.update(movie_params)
|
if @movie.update(movie_params)
|
||||||
redirect_to @movie, notice: "Movie successfully updated!"
|
redirect_to @movie, notice: "Movie successfully updated!"
|
||||||
else
|
else
|
||||||
@ -43,7 +42,6 @@ class MoviesController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
def destroy
|
def destroy
|
||||||
@movie = Movie.find(params[:id])
|
|
||||||
if @movie.destroy
|
if @movie.destroy
|
||||||
redirect_to movies_url, status: :see_other, alert: "Movie successfully destroyed!"
|
redirect_to movies_url, status: :see_other, alert: "Movie successfully destroyed!"
|
||||||
end
|
end
|
||||||
@ -51,6 +49,10 @@ class MoviesController < ApplicationController
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def set_movie
|
||||||
|
@movie = Movie.find_by!(slug: params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
def movie_params
|
def movie_params
|
||||||
params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross, :director, :duration, :image_file_name, genre_ids: [])
|
params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross, :director, :duration, :image_file_name, genre_ids: [])
|
||||||
end
|
end
|
||||||
|
@ -21,7 +21,7 @@ class ReviewsController < ApplicationController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def set_movie
|
def set_movie
|
||||||
@movie = Movie.find(params[:movie_id])
|
@movie = Movie.find_by!(slug: params[:movie_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
def review_params
|
def review_params
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
class Movie < ApplicationRecord
|
class Movie < ApplicationRecord
|
||||||
|
before_save :set_slug
|
||||||
|
|
||||||
validates :title, :released_on, :duration, presence: true
|
validates :title, :released_on, :duration, presence: true
|
||||||
validates :description, length: { minimum: 25 }
|
validates :description, length: { minimum: 25 }
|
||||||
validates :total_gross, numericality: { greater_than_or_equal_to: 0 }
|
validates :total_gross, numericality: { greater_than_or_equal_to: 0 }
|
||||||
@ -26,4 +28,14 @@ class Movie < ApplicationRecord
|
|||||||
def average_stars
|
def average_stars
|
||||||
reviews.average(:stars) || 0.0
|
reviews.average(:stars) || 0.0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_param
|
||||||
|
slug
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def set_slug
|
||||||
|
self.slug = title.parameterize
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
5
db/migrate/20230927133300_add_slug_to_movies.rb
Normal file
5
db/migrate/20230927133300_add_slug_to_movies.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class AddSlugToMovies < ActiveRecord::Migration[7.0]
|
||||||
|
def change
|
||||||
|
add_column :movies, :slug, :string
|
||||||
|
end
|
||||||
|
end
|
3
db/schema.rb
generated
3
db/schema.rb
generated
@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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|
|
create_table "characterizations", force: :cascade do |t|
|
||||||
t.integer "movie_id", null: false
|
t.integer "movie_id", null: false
|
||||||
t.integer "genre_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 "director"
|
||||||
t.string "duration"
|
t.string "duration"
|
||||||
t.string "image_file_name"
|
t.string "image_file_name"
|
||||||
|
t.string "slug"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "reviews", force: :cascade do |t|
|
create_table "reviews", force: :cascade do |t|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user