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
|
||||
|
@ -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…
Reference in new issue