I am following the very latest “Agile Web D. with Rails”
(second edition) book, and I am following the Depot tutorial. At the
end of “Task D” the tutorial challenges the reader to add the
functionality to be able to remove items from the cart. Why this
functionality was not included in the tutorial is beyond me, but that
is another discussion. My problem is that I can’t figure out how to
make this happen.
The main error that I am getting is that method quantity is not
defined. Or the cart will appear as unrendered HTML with the \n
(newline characters). It is very strange.
Any ideas?
The below code can be found at this link for easier reading:
http://pastie.caboo.se/10834
Note: I am only including the chuncks of code that pertain to the
cart item
removal functionality that I am trying to get to work.
– cart.rb
def remove_product(product)
@current_item = @items.find{|item| item.product == product}
if @current_item.quantity > 1
@current_item.quantity -= 1
elsif @current_item.quantity == 1
@items.delete(current_item)
end
@current_item
end
– store_controller.rb
def remove_from_cart
@product = Product.find(params[:id])
@cart = find_cart
@current_item = @cart.remove_product(@product)
redirect_to_index unless request.xhr?
end
– remove_from_cart.rjs
page[:cart].replace_html :partial => ‘cart’, :object => @cart
– _cart_item.rhtml (this code is contained withint <%= … %>
link_to_remote " - ", :update => “cart”, :url => {:action =>
:remove_from_cart, :id => cart_item.product}