Complete Lesson 25

main
Simon Quigley 1 year ago
parent 2e4e522c96
commit c3fbb70cff

@ -1,42 +1,42 @@
class MoviesController < ApplicationController class MoviesController < ApplicationController
def index def index
@movies = Movie.released @movies = Movie.released
end end
def show def show
@movie = Movie.find(params[:id]) @movie = Movie.find(params[:id])
end end
def edit def edit
@movie = Movie.find(params[:id]) @movie = Movie.find(params[:id])
end end
def update def update
@movie = Movie.find(params[:id]) @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
render :edit, status: :unprocessable_entity render :edit, status: :unprocessable_entity
end end
end end
def new def new
@movie = Movie.new @movie = Movie.new
end end
def create def create
@movie = Movie.new(movie_params) @movie = Movie.new(movie_params)
if @movie.save if @movie.save
redirect_to @movie, notice: "Movie successfully created!" redirect_to @movie, notice: "Movie successfully created!"
else else
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity
end end
end end
def destroy def destroy
@movie = Movie.find(params[:id]) @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
end end
private private
def movie_params def movie_params
params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross, :director, :duration, :image_file_name) params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross, :director, :duration, :image_file_name)
end end
end end

@ -1,2 +1,9 @@
module ReviewsHelper module ReviewsHelper
def average_stars(movie)
if movie.average_stars.zero?
content_tag(:strong, "No reviews")
else
pluralize(number_with_precision(movie.average_stars, precision: 1) , "star")
end
end
end end

@ -17,4 +17,7 @@ class Movie < ApplicationRecord
def self.released def self.released
where("released_on < ?", Time.now).order(released_on: :desc) where("released_on < ?", Time.now).order(released_on: :desc)
end end
def average_stars
reviews.average(:stars) || 0.0
end
end end

@ -12,6 +12,9 @@
<h3> <h3>
<%= total_gross(movie) %> <%= total_gross(movie) %>
</h3> </h3>
<span class="stars">
<%= average_stars(movie) %>
</span>
<p> <p>
<%= truncate(movie.description, length: 150, separator: ' ') %> <%= truncate(movie.description, length: 150, separator: ' ') %>
</p> </p>

@ -8,6 +8,7 @@
<%= year_of(@movie) %> &bull; <%= @movie.rating %> <%= year_of(@movie) %> &bull; <%= @movie.rating %>
</h2> </h2>
<div class="reviews"> <div class="reviews">
<%= average_stars(@movie) %>
<%= link_to pluralize(@movie.reviews.size, "review"), movie_reviews_path(@movie) %> <%= link_to pluralize(@movie.reviews.size, "review"), movie_reviews_path(@movie) %>
</div> </div>
<p> <p>

Loading…
Cancel
Save