Complete Lesson 37 Part 2

main
Simon Quigley 1 year ago
parent e8e6b38b3e
commit 38e5234b64

@ -1,2 +1,14 @@
class FavoritesController < ApplicationController 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 end

@ -6,6 +6,10 @@ class MoviesController < ApplicationController
end end
def show def show
@movie = Movie.find(params[:id]) @movie = Movie.find(params[:id])
@fans = @movie.fans
if current_user
@favorite = current_user.favorites.find_by(movie_id: @movie.id)
end
end end
def edit def edit
@movie = Movie.find(params[:id]) @movie = Movie.find(params[:id])

@ -13,6 +13,7 @@ class UsersController < ApplicationController
def show def show
@reviews = @user.reviews @reviews = @user.reviews
@favorite_movies = @user.favorite_movies
end end
def create def create

@ -1,2 +1,9 @@
module FavoritesHelper 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 end

@ -11,6 +11,7 @@ class Movie < ApplicationRecord
has_many :reviews, dependent: :destroy has_many :reviews, dependent: :destroy
has_many :favorites, dependent: :destroy has_many :favorites, dependent: :destroy
has_many :fans, through: :favorites, source: :user
def flop? def flop?
total_gross.blank? || total_gross < 225_000_000 total_gross.blank? || total_gross < 225_000_000

@ -3,6 +3,7 @@ class User < ApplicationRecord
has_many :reviews, dependent: :destroy has_many :reviews, dependent: :destroy
has_many :favorites, dependent: :destroy has_many :favorites, dependent: :destroy
has_many :favorite_movies, through: :favorites, source: :movie
validates :name, presence: true validates :name, presence: true
validates :email, presence: true, format: { with: /\S+@\S+/ }, uniqueness: { case_sensitive: false } validates :email, presence: true, format: { with: /\S+@\S+/ }, uniqueness: { case_sensitive: false }

@ -1,6 +1,14 @@
<section class="movie-details"> <section class="movie-details">
<div class="image"> <div class="image">
<%= image_tag @movie.image_file_name %> <%= image_tag @movie.image_file_name %>
<% if current_user %>
<div class="faves">
<%= fave_or_unfave_button(@movie, @favorite) %>
<div class="count">
<%= @fans.size %>
</div>
</div>
<% end %>
</div> </div>
<div class="details"> <div class="details">
<h1><%= @movie.title %></h1> <h1><%= @movie.title %></h1>
@ -36,4 +44,16 @@
<% end %> <% end %>
</section> </section>
</div> </div>
<aside>
<% if @fans.present? %>
<h4>Fans</h4>
<ul>
<% @fans.each do |user| %>
<li>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>
<% end %>
</aside>
</section> </section>

@ -1,11 +1,11 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :favorites
resource :session, only: [:new, :create, :destroy] resource :session, only: [:new, :create, :destroy]
resources :users resources :users
resources :reviews resources :reviews
root "movies#index" root "movies#index"
resources :movies do resources :movies do
resources :favorites, only: [:create, :destroy]
resources :reviews resources :reviews
end end

Loading…
Cancel
Save