Complete Lesson 35 Part 2

main
Simon Quigley 1 year ago
parent 29ac49b40d
commit 2461c5b015

@ -1,5 +1,6 @@
class ReviewsController < ApplicationController class ReviewsController < ApplicationController
before_action :set_movie before_action :set_movie
before_action :require_signin
def index def index
@reviews = @movie.reviews @reviews = @movie.reviews
@ -9,6 +10,7 @@ class ReviewsController < ApplicationController
end end
def create def create
@review = @movie.reviews.new(review_params) @review = @movie.reviews.new(review_params)
@review.user = current_user
if @review.save if @review.save
redirect_to movie_reviews_path(@movie), notice: "Thanks for your review!" redirect_to movie_reviews_path(@movie), notice: "Thanks for your review!"
else else
@ -23,6 +25,6 @@ class ReviewsController < ApplicationController
end end
def review_params def review_params
params.require(:review).permit(:name, :stars, :comment) params.require(:review).permit(:stars, :comment)
end end
end end

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

@ -5,7 +5,7 @@
<li> <li>
<%= pluralize(review.stars, 'star') %> <%= pluralize(review.stars, 'star') %>
<p> <p>
<%= review.name %> <%= link_to review.user.name, review.user %>
<span class="date"> <span class="date">
<%= time_ago_in_words(review.created_at) %> ago <%= time_ago_in_words(review.created_at) %> ago
</span> </span>

@ -3,9 +3,6 @@
<%= form_with(model: [@movie, @review]) do |f| %> <%= form_with(model: [@movie, @review]) do |f| %>
<%= render "shared/errors", object: @review %> <%= render "shared/errors", object: @review %>
<%= f.label :name %>
<%= f.text_field :name, autofocus: true %>
<%= f.label :stars %> <%= f.label :stars %>
<%= f.select :stars, [1, 2, 3, 4, 5], prompt: "Pick one" %> <%= f.select :stars, [1, 2, 3, 4, 5], prompt: "Pick one" %>

@ -7,4 +7,23 @@
<%= link_to "Delete Account", user_path(@user), class: "button delete", data: { turbo_method: :delete, turbo_confirm: "Permanently delete your account!?" } %> <%= link_to "Delete Account", user_path(@user), class: "button delete", data: { turbo_method: :delete, turbo_confirm: "Permanently delete your account!?" } %>
<% end %> <% end %>
</div> </div>
<% if @reviews.present? %>
<h3>Reviews</h3>
<% @reviews.each do |review| %>
<div class="review">
<div class="details">
<span class="title">
<%= link_to review.movie.title, review.movie %>
</span>
<%= pluralize(review.stars, 'star') %>
<span class="date">
<%= time_ago_in_words(review.created_at) %> ago
</span>
<p>
<%= review.comment %>
</p>
</div>
</div>
<% end %>
<% end %>
</section> </section>

Loading…
Cancel
Save