class StoreController < ApplicationController
def index
@products = Product.find_products_for_sale
+ @cart = find_cart
end
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
- @cart.add_product(product)
+ @current_item = @cart.add_product(product)
+ respond_to do |format|
+ format.js
+ end
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid product #{params[:id]}" )
redirect_to_index('Invalid product')
session[:cart] ||= Cart.new
end
- def redirect_to_index(msg)
- flash[:notice] = msg
+ def redirect_to_index(msg = nil)
+ flash[:notice] = msg if msg
redirect_to :action => 'index'
end
if current_item
current_item.increment_quantity
else
- @items << CartItem.new(product)
+ current_item = CartItem.new(product)
+ @items << current_item
end
+ current_item
end
def total_price
<head>
<title>Neil's Whimsical Online Store</title>
<%= stylesheet_link_tag "depot" , :media => "all" %>
+ <%= javascript_include_tag :defaults %>
</head>
<body id="store">
<div id="banner">
</div>
<div id="columns">
<div id="side">
+ <div id="cart">
+ <%= render(:partial => "cart" , :object => @cart) %>
+ </div>
<a href="http://www....">Home</a><br />
<a href="http://www..../faq">Questions</a><br />
<a href="http://www..../news">News</a><br />
--- /dev/null
+<div class="cart-title">Your Cart</div>
+ <table>
+ <%= render(:partial => 'cart_item', :collection => cart.items) %>
+
+ <tr class="total-line">
+ <td colspan="2">Total</td>
+ <td class="total-cell"><%= number_to_currency(cart.total_price, :unit => "£") %></td>
+ </tr>
+ </table>
+
+<%= button_to 'Empty cart', :action => :empty_cart %>
--- /dev/null
+<% if cart_item == @current_item %>
+ <tr id="current_item">
+<% else %>
+ <tr>
+<% end %>
+ <td><%= cart_item.quantity %>×</td>
+ <td><%=h cart_item.title %></td>
+ <td class="item-price"><%= number_to_currency(cart_item.price, :unit => "£") %></td>
+</tr>
+++ /dev/null
-<h2>Your Whimsical Cart</h2>
-
-<div class="cart-title">Your Cart</div>
- <table>
- <% for item in @cart.items %>
- <tr>
- <td><%= item.quantity %>×</td>
- <td><%=h item.title %></td>
- <td class="item-price"><%= number_to_currency(item.price, :unit => "£") %></td>
- </tr>
- <% end %>
- <tr class="total-line">
- <td colspan="2">Total</td>
- <td class="total-cell"><%= number_to_currency(@cart.total_price, :unit => "£") %></td>
- </tr>
- </table>
-
-<%= button_to 'Empty cart', :action => :empty_cart %>
--- /dev/null
+page.replace_html("cart", :partial => 'cart', :object => @cart)
+
+page[:current_item].visual_effect :highlight,
+ :startcolor => "#88ff88" ,
+ :endcolor => "#114411"
<%= product.description %>
<div class="price-line">
<span class="price"><%= number_to_currency product.price, :unit => "£" %></span>
- <%= button_to "Add to cart", :action => "add_to_cart", :id => product %>
+ <% form_remote_tag :url => {:action => 'add_to_cart', :id => product} do %>
+ <%= submit_tag "Add to cart" %>
+ <% end %>
</div>
</div>
<% end %>
}
/* END:cartmain */
+/* START:cartside */
+/* Styles for the cart in the sidebar */
+
+#cart, #cart table {
+ font-size: smaller;
+ color: white;
+}
+
+#cart table {
+ border-top: 1px dotted #595;
+ border-bottom: 1px dotted #595;
+ margin-bottom: 10px;
+}
+/* END:cartside */
+
+
/* The error box */