Remve/Update items in cart

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

I am working on the same code at the moment. You should set up a few methods in your CartItem class -- check the book about that. Also, some of your code below is not exactly as it is the book -- but I don't know enough to tell you where your problem is.

Good luck, Elle

I am not actually working on the depot app, and never have, but I took a quick look at it. Try adding this to your cart class:

`def remove_from_cart(item)

@items.delete(item)

end

`Then in your controller:

  def remove_item
@cart = find_cart
product = Product.find params[:id]
if product
@cart.remove_from_cart(product)
flash[:notice] = "Item has been removed from cart"
else
flash[:error] = "Unable to find product"
end
redirect_to(:action => :display_cart)
end

Since I don’t have a depot app to test with, I can’t guarantee this all works perfectly, but it should point you in the right direction.

-Bill

elle wrote:

That solution didnt work. It did not produce any errors but didnt do anything either. Also I have the first editien of the book and that may be why my code looks different.

If you are getting that error, then you did not put the remove_from_cart method I included in models/cart.rb or you misspelled it. If not, try restarting your server.

wrote:

I'm not getting any errors, it just is'nt working.

Then what was this in your post?

undefined method `remove_from_cart' for #<Cart:0x2aaaab864040>

Looks like an error to me?

wrote:

Yeah I accidently posted that. It was because I made a typo but i fixed it and the code doesnt do anything.

You will need to paste your model and controller code for me to see what you are doing wrong. Paste it to pastie.caboo.se and send the link if you want me to look further.

wrote:

Parked at Loopia is the link. It has all three relevant files. Pleas let me know what you think. I've also discovered that I can set the quantity to zero and the item will still be in the cart with the new quantity. Below is the code responsible for adding items, by changing the one to the desired quantity it will update but a quantity of zero will not remove it.

  def add_product(product)     item = @items.find {|i| i.product_id == product.id}     if item       item.quantity += 1     else       item = LineItem.for_product(product)       @items << item     end     @total_price += product.price   end

NM I aftir looxinc at the code again I figured it out on my own. Here is the solution:

<%= link_to("<font color=red>(remove)</font>", :action => :remove_item, :id => product.id) %>

  def remove_item     product = Product.find(params[:id])     @cart.del_product(product)     flash[:notice] = "Item has been removed"     redirect_to(:action => :display_cart)   end

  def del_product(product)     item = @items.find {|i| i.product_id == product.id}     temp_price = product.price * item.quantity     @items.delete(item)     @total_price -= temp_price   end