I have been working through the Agile Web D. with Rails book
doing the Depot tutorial. I have completed it and it works but I cant
say I understand how and why.
My question is this “After adding products to the Cart, how do I then
give users the option to delete just one item and not empty the whole
cart?”
I’ve been playing around with all sorts of code, just making myself more
confused.
Any suggestions very much appreciated
If you don’t understand most of the tutorial then I think it you
should consider following some other tutorials or better yet
designing your own app… that will force you to understand what you
are doing. Don’t worry, you’re not alone, it’s taken me a long time
to wrap my head around RonR.
Anyway, to remove an item, I added this to my controller:
def remove_item @cart = find_cart @cart.remove_cart_item(params[:id])
end
And this to the cart model:
Method to remove a product/option from the cart
def remove_cart_item(id)
if @items.delete_if {|item| item.id == id.to_i }
true
else
false
end
end
And finally I modified the initialize function in the CartItem class
like so, so that each item in the @items array of the cart object
would have a unique id:
Let me know if you don’t know what any of this code is doing.
Basically it just removes the appropriate item from the @items array
in the cart object based on the item id.
Hi Sean,
Thanks for that.
I understand what your code is doing which is about where I am with most
of Rails. How to get it to work is another matter.
I assume by this
And finally I modified the initialize function in the CartItem class
like so, so that each item in the @items array of the cart object
would have a unique id:
Sure, for each row in the table produced on the display cart page you
can just add another column at the end with a link in it. So you just
need to pass the correct action an id to the link_to helper.
The initialize method I’m referring to is in the CartItem class, not
the Cart class. If you don’t have a CartItem class yet I guess it
means it makes it’s appearance a bit later in the tutorial.
Take care,
Sean
Maybe we’re reading from different books, mines printed February 2006,
as I’m up to the chapter on Testing and there isn’t any mention of
CartItem (but I do have a LineItem class). I’ve looked through the
completed code in the back of the book to make sure I haven’t missed
anything.
Anyway, thanks for your advice. I’ll have a go at including it in my
code.
Sure, for each row in the table produced on the display cart page you
can just add another column at the end with a link in it. So you just
need to pass the correct action an id to the link_to helper.
The initialize method I’m referring to is in the CartItem class, not
the Cart class. If you don’t have a CartItem class yet I guess it
means it makes it’s appearance a bit later in the tutorial.
Yep, I was referring the the 2nd edition, which is currently
available in beta form. There’s a lot of improvements an additions
already in the beta of the 2nd edition. I think it’s worth upgrading.
Take care,
Sean
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.