Complete Lesson 24

main
Simon Quigley 8 months ago
parent 9a1b2b031c
commit 2e4e522c96

@ -1,6 +1,28 @@
class ReviewsController < ApplicationController
before_action :set_movie
def index
@movie = Movie.find(params[:movie_id])
@reviews = @movie.reviews
end
def new
@review = @movie.reviews.new
end
def create
@review = @movie.reviews.new(review_params)
if @review.save
redirect_to movie_reviews_path(@movie), notice: "Thanks for your review!"
else
render :new, status: :unprocessable_entity
end
end
private
def set_movie
@movie = Movie.find(params[:movie_id])
end
def review_params
params.require(:review).permit(:name, :stars, :comment)
end
end

@ -27,6 +27,7 @@
<td><%= total_gross(@movie) %></td>
</tr>
</table>
<%= link_to "Write Review", new_movie_review_path(@movie), class: "review" %>
<section class="admin">
<%= link_to "Edit", edit_movie_path(@movie), class: "button" %>
<%= link_to "Delete", movie_path(@movie), class: "button", data: { turbo_method: :delete, turbo_confirm: "Are you sure?" } %>

@ -0,0 +1,16 @@
<h1>New Review for <%= link_to @movie.title, @movie %></h1>
<%= form_with(model: [@movie, @review]) do |f| %>
<%= render "shared/errors", object: @review %>
<%= f.label :name %>
<%= f.text_field :name, autofocus: true %>
<%= f.label :stars %>
<%= f.select :stars, [1, 2, 3, 4, 5], prompt: "Pick one" %>
<%= f.label :comment %>
<%= f.text_area :comment, placeholder: "What did you think?" %>
<%= f.submit "Post Review" %>
<% end %>
Loading…
Cancel
Save