# Newbie question about Agile Rails book - Delete from cart

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-agile-rails-book-delete-from-cart/64467>\
**Category:** Rails\
**Created:** [June 26, 2006, 5:45am UTC](https://www.ruby-forum.com/t/newbie-question-about-agile-rails-book-delete-from-cart/64467 "2006-06-26T05:45:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [June 26, 2006, 5:45am UTC](https://www.ruby-forum.com/t/newbie-question-about-agile-rails-book-delete-from-cart/64467/1 "2006-06-26T05:45:01Z")

</div>

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

Bob

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [June 26, 2006, 6:03am UTC](https://www.ruby-forum.com/t/newbie-question-about-agile-rails-book-delete-from-cart/64467/2 "2006-06-26T06:03:03Z")

</div>

Hi Bob,

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:

def initialize(product,option)  
@product = product unless product.nil?  
@option = option unless option.nil?  
@quantity = 1  
@id = Time.now.to\_i  
end

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.

Take care,  
Sean

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [June 26, 2006, 6:51am UTC](https://www.ruby-forum.com/t/newbie-question-about-agile-rails-book-delete-from-cart/64467/3 "2006-06-26T06:51:57Z")

</div>

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:
> 
> def initialize(product,option)  
> @product = product unless product.nil?  
> @option = option unless option.nil?  
> @quantity = 1  
> @id = Time.now.to\_i  
> end

you mean the initialize function in Cart.rb as you say “modify” and  
thats the only occurrence of initialize.

Can you suggest how to code a ‘remove item’ link on the  
display\_cart.rhtml page.

Bob

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [June 26, 2006, 7:50am UTC](https://www.ruby-forum.com/t/newbie-question-about-agile-rails-book-delete-from-cart/64467/4 "2006-06-26T07:50:48Z")

</div>

Sean O’Hara wrote:

> Hi Bob,
> 
> 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.

Bob

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [June 26, 2006, 7:35am UTC](https://www.ruby-forum.com/t/newbie-question-about-agile-rails-book-delete-from-cart/64467/5 "2006-06-26T07:35:59Z")

</div>

Hi Bob,

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

---

<div class="post-metadata">

**Author:** ![bob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea666f/32.png) [@bob](https://www.ruby-forum.com/u/bob)\
**Post date:** [June 26, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/newbie-question-about-agile-rails-book-delete-from-cart/64467/6 "2006-06-26T17:26:47Z")

</div>

Hi Bob,

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
