Newby Question

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

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.

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

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

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

On 11/15/06, Willem H. [email protected] 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: [email protected]