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 -%>
- <%= link_to 'Continue shopping', :action => "index" %>
- <%= link_to 'Empty cart', :action => "empty_cart" %>
- <%= link_to 'Checkout', :action => "checkout" %>
Description | Qty<product | ||
Price Each | Total | ||
<%= h(product.title) %> <%= link_to("(remove)", :action => :remove_item, :id => product.id) %> | <%= item.quantity %> | <%= fmt_dollars(item.unit_price) %> | <%= fmt_dollars(item.unit_price * item.quantity) %> |
Total: | <%= fmt_dollars(@cart.total_price) %> |
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