I purchase che Agile Web Develoment with rails book so that i could start learning RoR. I've worked through the depot applicationand it works fine. I've even went as far ao to add more features. The Problem is I can,t figure out how to edit/remove items frm the cart.
Here is my shopping cart view:
<% @page_title = "h3avystore cart" -%> <% unless params[:context] == :checkout -%> <div id=cartmenu> <ul> <li><%= link_to 'Continue shopping', :action => "index" %></li> <li><%= link_to 'Empty cart', :action => "empty_cart" %></li> <li><%= link_to 'Checkout', :action => "checkout" %></li> </ul> </div> <% end -%>
<table cellpadding=10 cellspacing=0 width=75%> <tr class=carttitle> <td rowspan=2>Description</td> <td rowspan=2>Qty<product <td colspan=2></td> </tr> <tr class=carttitle> <td align=right>Price Each</td> <td align=right>Total</td> </tr> <% for item in @items product = item.product -%> <tr> <td><%= h(product.title) %> <%= link_to("(remove)", :action => :remove_item, :id => product.id) %></td> <td align=center><%= item.quantity %></td> <td align=right><%= fmt_dollars(item.unit_price) %></td> <td align=right><%= fmt_dollars(item.unit_price * item.quantity) %></td> </tr> <% end %> <tr> <td colspan=3 align=right><strong>Total:</strong></td> <td id=totalcell><%= fmt_dollars(@cart.total_price) %></td> </tr> </table>
Here is my store contraller:
class StoreController < ApplicationController
before_filter :find_cart, :except => :index
def index @products = Product.salable_items end
def add_to_cart product = Product.find(params[:id]) @cart = find_cart @cart.add_product(product) redirect_to(:action => 'display_cart') rescue logger.error("Attempt to access invalid product #{params[:id]}") redirect_to_index('Invalid product') end
def display_cart @items = @cart.items if @items.empty? redirect_to_index("Your cart is currently empty") end if params[:context] == :checkout render(:layout => false) end end
def remove_item id = params[:id] flash[:notice] = "Item has been removed from cart" redirect_to(:action => :display_cart) end
def empty_cart @cart = find_cart @cart.empty! redirect_to_index('Your cart is now empty') end
def checkout @items = find_cart.items if @items.empty? redirect_to_index('There is nothing in your cart!') else @order = Order.new end end
def save_order @cart = find_cart @order = Order.new(params[:order]) @order.line_items << @cart.items if @order.save @cart.empty! redirect_to_index('Thank you for your order') else render(:action => 'checkout') end end
private
def find_cart @cart = (session[:cart] ||= Cart.new) end end
Thats as much as i can get working. The id is getting passed and in the correct produt but I cant figure out how to access that prduct to change the quantity. Any help would be greatly appreciated. TIA