Skip to content

Commit

Permalink
Update update_review to user AJAX.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelsey Langlois and LDevinMacKrell committed Dec 6, 2017
1 parent 280cf74 commit 65de1d0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
5 changes: 4 additions & 1 deletion app/controllers/reviews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def create
@review.account = current_user.account
if @review.save
flash[:notice] = "You have successfully reviewed this book!"
redirect_to product_path(@product)
respond_to do |format|
format.html { redirect_to product_path(@product) }
format.js
end
else
flash[:alert] = @review.errors.full_messages
render :new
Expand Down
2 changes: 1 addition & 1 deletion app/views/reviews/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="form">
<%= form_for [@product, @review] do |f| %>
<%= form_for [@product, @review], remote: true do |f| %>
<div class="form-group">
<%= f.label :content %>
<%= f.text_area :content, class: 'form-control' %>
Expand Down
24 changes: 5 additions & 19 deletions app/views/reviews/_index.html.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
<h2>Reviews</h2>
<% if @product.reviews.any? %>
<% @product.reviews.each do |review| %>
<div class="review">
<h3>By <%= review.account.user.email %></h3>
<p><%= review.content %></p>
<% if current_user %>
<p>
<% if review.account == current_user.account %>
<%= link_to "Edit", edit_product_review_path(@product, review) %> |
<% end %>
<% if (review.account == current_user.account) || current_user.admin? %>
<%= link_to "Delete", product_review_path(@product, review), data: {
confirm: "Are you sure you want to incinerate this review with your blaster ray?",
method: "delete"
} %>
<% end %>
</p>
<% end %>
</div>
<% end %>
<div id="reviews">
<% @product.reviews.each do |review| %>
<%= render "reviews/review", review: review %>
<% end %>
</div>
<% else %>
<h3>There are no saved reviews for this book!</h3>
<p>Be the first to voyage where no man has gone before!</p>
Expand Down
17 changes: 17 additions & 0 deletions app/views/reviews/_review.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="review">
<h3>By <%= review.account.user.email %></h3>
<p><%= review.content %></p>
<% if current_user %>
<p>
<% if review.account == current_user.account %>
<%= link_to "Edit", edit_product_review_path(@product, review) %> |
<% end %>
<% if (review.account == current_user.account) || current_user.admin? %>
<%= link_to "Delete", product_review_path(@product, review), data: {
confirm: "Are you sure you want to incinerate this review with your blaster ray?",
method: "delete"
} %>
<% end %>
</p>
<% end %>
</div>
3 changes: 3 additions & 0 deletions app/views/reviews/create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$('#new_review').remove();
$('#new_link').show();
$('#reviews').append('<%= j render(@review) %>');

0 comments on commit 65de1d0

Please sign in to comment.