Forum: Ruby on Rails "Undefined method" problem in "Agile Web Development With Ra

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Emil Gustafsson (Guest)
on 2006-03-05 22:34
(Received via mailing list)
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: </tr>
26: <%
27: for item in @items
28: 	product = item.product
29: -%>
30: <tr>
31: 	<td><%= h(product.title) %></td>


I'm sure my source code is correct.

//Emil
David M. (Guest)
on 2006-03-05 22:43
(Received via mailing list)
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.
Emil Gustafsson (Guest)
on 2006-03-05 23:50
(Received via mailing list)
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: <td><%= h(product.title) %></td>
>>
>>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails



Hälsningar,
Emil

removed_email_address@domain.invalid
Josh K. (Guest)
on 2006-03-22 06:39
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.
This topic is locked and can not be replied to.