Complete Lesson 40
This commit is contained in:
parent
9fe5f43726
commit
20a64b9033
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
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.
|
||||
|
||||
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|
|
||||
|
Loading…
x
Reference in New Issue
Block a user