Complete Lesson 37 Part 2

main
Simon Quigley 1 year ago
parent e8e6b38b3e
commit 38e5234b64

@ -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

@ -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])

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

@ -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

@ -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

@ -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 }

@ -1,6 +1,14 @@
<section class="movie-details">
<div class="image">
<%= 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 class="details">
<h1><%= @movie.title %></h1>
@ -36,4 +44,16 @@
<% end %>
</section>
</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>

@ -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

Loading…
Cancel
Save