From 38e5234b64c3d95d8366fc7f155b617fbbd15a4d Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Wed, 27 Sep 2023 08:02:41 -0500 Subject: [PATCH] Complete Lesson 37 Part 2 --- app/controllers/favorites_controller.rb | 12 ++++++++++++ app/controllers/movies_controller.rb | 4 ++++ app/controllers/users_controller.rb | 1 + app/helpers/favorites_helper.rb | 7 +++++++ app/models/movie.rb | 1 + app/models/user.rb | 1 + app/views/movies/show.html.erb | 20 ++++++++++++++++++++ config/routes.rb | 2 +- 8 files changed, 47 insertions(+), 1 deletion(-) diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index f45ad6e..40385a5 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -1,2 +1,14 @@ class FavoritesController < ApplicationController + before_action :require_signin + def create + @movie = Movie.find(params[:movie_id]) + @movie.favorites.create!(user: current_user) + redirect_to @movie + end + def destroy + favorite = current_user.favorites.find(params[:id]) + favorite.destroy + + redirect_to Movie.find(params[:movie_id]) + end end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 2596103..bc0f6e0 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -6,6 +6,10 @@ class MoviesController < ApplicationController end def show @movie = Movie.find(params[:id]) + @fans = @movie.fans + if current_user + @favorite = current_user.favorites.find_by(movie_id: @movie.id) + end end def edit @movie = Movie.find(params[:id]) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 8b34500..34f5b4f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -13,6 +13,7 @@ class UsersController < ApplicationController def show @reviews = @user.reviews + @favorite_movies = @user.favorite_movies end def create diff --git a/app/helpers/favorites_helper.rb b/app/helpers/favorites_helper.rb index 4e9a950..b888a94 100644 --- a/app/helpers/favorites_helper.rb +++ b/app/helpers/favorites_helper.rb @@ -1,2 +1,9 @@ module FavoritesHelper + def fave_or_unfave_button(movie, favorite) + if favorite + button_to "♡ Unfave", movie_favorite_path(movie, favorite), method: :delete + else + button_to "♥️ Fave", movie_favorites_path(movie) + end + end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 2803a37..f45e491 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -11,6 +11,7 @@ class Movie < ApplicationRecord has_many :reviews, dependent: :destroy has_many :favorites, dependent: :destroy + has_many :fans, through: :favorites, source: :user def flop? total_gross.blank? || total_gross < 225_000_000 diff --git a/app/models/user.rb b/app/models/user.rb index b7f7288..c408f06 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,6 +3,7 @@ class User < ApplicationRecord has_many :reviews, dependent: :destroy has_many :favorites, dependent: :destroy + has_many :favorite_movies, through: :favorites, source: :movie validates :name, presence: true validates :email, presence: true, format: { with: /\S+@\S+/ }, uniqueness: { case_sensitive: false } diff --git a/app/views/movies/show.html.erb b/app/views/movies/show.html.erb index 4c827df..9fa5d0e 100644 --- a/app/views/movies/show.html.erb +++ b/app/views/movies/show.html.erb @@ -1,6 +1,14 @@
<%= image_tag @movie.image_file_name %> + <% if current_user %> +
+ <%= fave_or_unfave_button(@movie, @favorite) %> +
+ <%= @fans.size %> +
+
+ <% end %>

<%= @movie.title %>

@@ -36,4 +44,16 @@ <% end %>
+ diff --git a/config/routes.rb b/config/routes.rb index 38da86e..5326b26 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,11 @@ Rails.application.routes.draw do - resources :favorites resource :session, only: [:new, :create, :destroy] resources :users resources :reviews root "movies#index" resources :movies do + resources :favorites, only: [:create, :destroy] resources :reviews end