# Newby Question

**URL:** <https://www.ruby-forum.com/t/newby-question/75939>\
**Category:** Rails\
**Created:** [November 14, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/newby-question/75939 "2006-11-14T15:10:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willem\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c77e96/32.png) [@Willem\_H](https://www.ruby-forum.com/u/Willem_H)\
**Post date:** [November 14, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/newby-question/75939/1 "2006-11-14T15:10:37Z")

</div>

I am following the Depot tutorial in Agile Web D. with Rail,

I am getting the following error,

NoMethodError in StoreController#empty\_cart  
undefined method `empty!’ for #Cart:0x5012250

Here is my call in store\_controller.rb,  
def empty\_cart  
find\_cart.empty!  
flash[:notice] = ‘Your cart is now empty’  
redirect\_to(:action =\> ‘index’)  
end

And in my Model cart.rb,

def empty!  
@items = []  
@total\_price = 0.0  
end

---

<div class="post-metadata">

**Author:** ![Willem\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c77e96/32.png) [@Willem\_H](https://www.ruby-forum.com/u/Willem_H)\
**Post date:** [November 14, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/newby-question/75939/2 "2006-11-14T15:36:41Z")

</div>

It looks like you might have the empty! method after the private  
declaration. If so, it will not be visible to other objects.

I always name all the private methods with an underscore in the  
beginning.  
Just by looking at the code I know which methods are private and will  
catch  
this kind of errors quickly.

---

<div class="post-metadata">

**Author:** ![Willem\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c77e96/32.png) [@Willem\_H](https://www.ruby-forum.com/u/Willem_H)\
**Post date:** [November 14, 2006, 7:03pm UTC](https://www.ruby-forum.com/t/newby-question/75939/3 "2006-11-14T19:03:23Z")

</div>

well if that is copy/paste code then i got to ask what the hell is  
find\_cart. there is no instantiation

---

<div class="post-metadata">

**Author:** ![Willem\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c77e96/32.png) [@Willem\_H](https://www.ruby-forum.com/u/Willem_H)\
**Post date:** [November 15, 2006, 7:15am UTC](https://www.ruby-forum.com/t/newby-question/75939/4 "2006-11-15T07:15:00Z")

</div>

Well here is a direct copy,

# From store\_controller.rb:

class StoreController \< ApplicationController  
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  
@cart = find\_cart  
@items = @cart.items  
if @items.empty?  
redirect\_to\_index(“Your cart is currently empty”)  
end  
end

def empty\_cart  
@cart = find\_cart  
@cart.empty!  
redirect\_to\_index(‘Your cart is now empty’)  
end  
PAGE 97  
def checkout  
@cart = find\_cart  
@items = @cart.items  
if @items.empty?  
redirect\_to\_index(“There’s nothing in your cart!”)  
else  
@order = Order.new  
end  
end

private  
def redirect\_to\_index(msg = nil)  
flash[:notice] = msg if msg  
redirect\_to(:action =\> ‘index’)  
end

private  
def find\_cart  
session[:cart] ||= Cart.new  
end  
end

# ====================== Then From the model cart.rb:

class Cart  
attr\_reader :items  
attr\_reader :total\_price

def initialize  
empty!  
end

def empty!  
@items = []  
@total\_price = 0.0  
end

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 end

---

<div class="post-metadata">

**Author:** ![Willem\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c77e96/32.png) [@Willem\_H](https://www.ruby-forum.com/u/Willem_H)\
**Post date:** [November 15, 2006, 7:35am UTC](https://www.ruby-forum.com/t/newby-question/75939/5 "2006-11-15T07:35:01Z")

</div>

Never mind this morning when i started up firefox the error just isn’t  
there anymore, very strange.

On 11/15/06, Willem H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> product = Product.find(params[:id])
> @items = @cart.items
> 
> ```
> 
> PAGE 97  
> private
> 
> ```
> else
> item = LineItem.for_product(product)
> @items << item
> end
> 
> @total_price += product.price
> 
> ```
> 
> # end end

–  
Willem H.  
OSS Developer  
Business Data Solutions  
Tel #: 021 422 09 51  
E-mail: removed\_email\_address@domain.invalid
