Browse files

Deleting the item and cleaning the cart with ajax

  • Loading branch information...
1 parent 5ada3c2 commit 8e55bcca8557f986f8c584537b3985e3e040a4f2 @flexoid committed Feb 26, 2012
View
1 app/controllers/carts_controller.rb
@@ -89,6 +89,7 @@ def destroy
respond_to do |format|
format.html { redirect_to store_url,
notice: 'Your cart is currently empty' }
+ format.js
format.json { head :no_content }
end
end
View
1 app/controllers/line_items_controller.rb
@@ -94,6 +94,7 @@ def destroy
respond_to do |format|
format.html { redirect_to store_url }
+ format.js
format.json { head :no_content }
end
end
View
1 app/controllers/store_controller.rb
@@ -1,6 +1,7 @@
class StoreController < ApplicationController
def index
@products = Product.order(:title)
+ @cart = current_cart
if session[:counter].nil?
session[:counter] = 1
View
2 app/views/carts/_cart.html.erb
@@ -8,5 +8,5 @@
</tr>
</table>
-<%= button_to 'Empty cart', cart, method: :delete, confirm: 'Are you sure?' %>
+<%= button_to 'Empty cart', cart, method: :delete, confirm: 'Are you sure?', remote: true %>
View
1 app/views/carts/destroy.js.erb
@@ -0,0 +1 @@
+$('#cart').hide('blind', 1000);
View
6 app/views/layouts/application.html.erb
@@ -17,8 +17,10 @@
<div id="side">
<span id="datetime"><%= date_time %></span>
- <%= hidden_div_if(current_cart.line_items.empty?, id: 'cart') do %>
- <%= render current_cart %>
+ <% if @cart %>
+ <%= hidden_div_if(current_cart.line_items.empty?, id: 'cart') do %>
+ <%= render current_cart %>
+ <% end %>
<% end %>
<ul>
View
2 app/views/line_items/_line_item.html.erb
@@ -8,6 +8,6 @@
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
<td>
- <%= button_to 'Delete', line_item, method: :delete, confirm: 'Are you sure?' %>
+ <%= button_to 'Delete', line_item, method: :delete, remote: true %>
</td>
</tr>
View
4 app/views/line_items/create.js.erb
@@ -1,6 +1,6 @@
-if ($('#cart tr').length == 1) { $('#cart').show('blind', 1000); }
+$('#cart').html("<%=j render current_cart %>");
-$('#cart').html("<%=j render @cart %>");
+if ($('#cart tr').is(":visible") == false) { $('#cart').show('blind', 1000); }
$('#current_item').css({'background-color': '#88ff88'})
.animate({'background-color': '#114411'}, 1000);
View
5 app/views/line_items/destroy.js.erb
@@ -0,0 +1,5 @@
+<% if current_cart.line_items.empty? %>
+ $('#cart').hide('blind', 1000);
+<% else %>
+ $('#cart').html("<%=j render current_cart %>");
+<% end %>

0 comments on commit 8e55bcc

Please sign in to comment.