"Undefined method" problem in "Agile Web Development With Ra


#1

Hi,

I’m reading the “Agile Web D. With Rails” book. So long,
everything has worked fine, but now I got this annoying error message
that I just can’t understand. The error appears for me around page
87, chapter 8.

NoMethodError in Store#display_cart

Showing app/views/store/display_cart.rhtml where line #28 raised:

undefined method `product’ for 2395.0:Float

Extracted source (around line #28):

25:
26: <%
27: for item in @items
28: product = item.product
29: -%>
30:


31: <%= h(product.title) %>

I’m sure my source code is correct.

//Emil


#2

For some reason your object “item” has the value 2395.0, and is a
floating point object. Without having the Agile book at my
fingertips, from memory it’s supposed to have a price attribute with
that value.

At a guess, you could be setting @items to an individual item, rather
than a collection of items. That would be consistent with the error
you’re seeing.

Have a look at your controller code where you set @items to something;
the problem’s probably going to be right around there.

HTH

Dave M.


#3

This is the code in the controller where @items is set:

def display_cart
@cart = find_cart
@items = @cart.items
end

private
def find_cart
session[:cart] ||= Cart.new
end

And this is the code in the cart.rb model:

class Cart

attr_reader :items
attr_reader :total_price

def initialize
@items = []
@total_price = 0.0
end

def add_product(product)
@items << LineItem.for_product(product)
@total_price += product.price
end
end

Any idea?

5 mar 2006 kl. 21.43 - 5 mar skrev David M.:

the problem’s probably going to be right around there.

has worked fine, but now I got this annoying error message that I

31:

<%= h(product.title) %>

Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Hälsningar,
Emil

removed_email_address@domain.invalid


#4

I am working through that book as well. I am getting the exact same
error. I thought it was something I did wrong but I have no idea what to
check since I’m still pretty new at this stuff.

Hopefully, it’s the syntax of the book. Has anyone seen the errata or
have any ideas? I would think that if we’re getting the same results,
surely there’s someone else out there who knows what’s going on.