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 -%>

  • <%= link_to 'Continue shopping', :action => "index" %>
  • <%= link_to 'Empty cart', :action => "empty_cart" %>
  • <%= link_to 'Checkout', :action => "checkout" %>
<% end -%> <% for item in @items product = item.product -%> <% end %>
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

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

That method produces no errors but also does nothing. It should also
be montioned I have the first edition of the book and thats probably
why my codo looks different.

undefined method `remove_from_cart’ for #Cart:0x2aaaab864040

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.

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

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:

    Qty<product <%= fmt_dollars(@cart.total_price) %> @products = Product.salable_items end

    end
    def save_order


    Sincerely,

    William P.

Then what was this in your post?

undefined method `remove_from_cart’ for #Cart:0x2aaaab864040

Looks like an error to me?

[email protected] wrote:

That method produces no errors but also does nothing. It should also

def remove_from_cart(item)
flash[:notice] = “Item has been removed from cart”

<% unless params[:context] == :checkout -%>

product = item.product
logger.error("Attempt to access invalid product #{params[:id]}")
end
@cart.empty!

end
end
the correct produt but I cant figure out how to access that prduct to

Sincerely,

William P.

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.

[email protected] wrote:

Looks like an error to me?

method I included in models/cart.rb or you misspelled it. If not, try

On Oct 19, 7:33 pm, William P. [email protected] wrote:

def remove_item

Also, some of your code below is not exactly as it is the book – but

is I can,t figure out how to edit/remove items frm the cart.

Price Each <%= fmt_dollars(item.unit_price) %> Here is my store contraller: product = Product.find(params[:id]) if @items.empty? redirect_to(:action => :display_cart) if @items.empty? if @order.save @cart = (session[:cart] ||= Cart.new)

William P.

Sincerely,

William P.

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

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("(remove)", :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

Parked at Loopia is the link.