# Same error ... again

**URL:** <https://www.ruby-forum.com/t/same-error-again/100983>\
**Category:** Rails\
**Created:** [August 2, 2006, 9:54am UTC](https://www.ruby-forum.com/t/same-error-again/100983 "2006-08-02T09:54:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 2, 2006, 9:54am UTC](https://www.ruby-forum.com/t/same-error-again/100983/1 "2006-08-02T09:54:06Z")

</div>

I reached page 82 of the Book “Agile Development with Rails” First  
Edition. After I compiled the code I got the following error message.

[CODE]  
NoMethodError in Store#display\_cart  
Showing app/views/store/display\_cart.rhtml where line #11 raised:

undefined method `product’ for 1099.0:Float

8:

  
9: \<%  
10: for item in @items  
11: product = item.product  
12: -%\>  
13:   
14: 

[CODE]

My display\_cart.rhtml from views/store/ is as follows:

[CODE]

# Display Cart

| \<%= item.quantity %\> |

\<% for item in @items product = item.product -%\> \<% end -%\>

| \<%= item.quantity %\> | \<%= h(product.title) %\> | \<%= item.unit\_price %\> | \<%= item.unit\_price \* item.quantity %\> |

[CODE]

My store\_controller.rb in app/controllers/ is:

[CODE]  
class StoreController \< ApplicationController

def index  
@products = Product.salable\_items #salable items defined in  
product.rb  
end #model

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
redirect\_to(:action =\> ‘display\_cart’)  
end  
def display\_cart  
@cart = find\_cart  
@items = @cart.items  
end  
private #prevents making the method available as an action on the  
controller  
def find\_cart  
session[:cart] ||= Cart.new #If the session hash has a value  
corresponding  
#to the key :cart, that  
#value is returned immediately.  
#Otherwise a new cart object is created  
and  
#assigned to the session.  
#This new cart is then returned.  
end

end

[CODE]

and finally my cart.rb which rests in app/models:

[CODE]  
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

[CODE]

What is the problem that it gives me that float error? Eeverything seems  
to be in order.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 2, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/same-error-again/100983/2 "2006-08-02T15:18:08Z")

</div>

curtis wrote:

> What you could do is add a \<%= debug(item) %\> somewhere  
> in between line 12 and 13 and that will should you what’s accessible  
> from the item object. You could also do a \<%= item.product.class %\> to  
> see if it’s the proper class.
> 
> curtis  
> [http://www.cookmor.com](http://www.cookmor.com)
> 
> N/a N/a wrote:
> 
> > I reached page 82 of the Book “Agile Development with Rails” First  
> > Edition. After I compiled the code I got the following error message.
> > 
> > [CODE]  
> > NoMethodError in Store#display\_cart  
> > Showing app/views/store/display\_cart.rhtml where line #11 raised:
> > 
> > undefined method `product’ for 1099.0:Float
> > 
> > 8:
> > 
> >   
> > 9: \<%  
> > 10: for item in @items  
> > 11: product = item.product  
> > 12: -%\>  
> > 13:   
> > 14: 
> > 
> > [CODE]
> > 
> > I cannot add inbetween lines 12 and 13 beacause the error-code is  
> > executed allready and it crashes at “product = item.product” and it does  
> > not execute line 12 and 13. I added both of the lines inbetween line 8  
> > and line 9. And it always crashes at line 9 (\<%= debug(item) %\>) with  
> > the following error “undefined local variable or method `item’ for  
> > #\<#Class:0x3089b68:0x30891e0\>”.
> > 
> > | \<%= item.quantity %\> |

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 2, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/same-error-again/100983/3 "2006-08-02T14:32:20Z")

</div>

What you could do is add a \<%= debug(item) %\> somewhere  
in between line 12 and 13 and that will should you what’s accessible  
from the item object. You could also do a \<%= item.product.class %\> to  
see if it’s the proper class.

curtis  
[http://www.cookmor.com](http://www.cookmor.com)

N/a N/a wrote:

> I reached page 82 of the Book “Agile Development with Rails” First  
> Edition. After I compiled the code I got the following error message.
> 
> [CODE]  
> NoMethodError in Store#display\_cart  
> Showing app/views/store/display\_cart.rhtml where line #11 raised:
> 
> undefined method `product’ for 1099.0:Float
> 
> 8:
> 
>   
> 9: \<%  
> 10: for item in @items  
> 11: product = item.product  
> 12: -%\>  
> 13:   
> 14: 
> 
> [CODE]
> 
> | \<%= item.quantity %\> |

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 3, 2006, 7:35am UTC](https://www.ruby-forum.com/t/same-error-again/100983/4 "2006-08-03T07:35:54Z")

</div>

Can anyone help? I really reached a deadline here.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 3, 2006, 12:16pm UTC](https://www.ruby-forum.com/t/same-error-again/100983/5 "2006-08-03T12:16:27Z")

</div>

Hi,

what does your for\_product class method on the LineItem Class look like.  
Please post your LineItem-class as well.

Without that information one can’t tell IMHO how the @items variable  
gets  
filled with Floats.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 3, 2006, 12:31pm UTC](https://www.ruby-forum.com/t/same-error-again/100983/6 "2006-08-03T12:31:50Z")

</div>

curtis wrote:

> > I reached page 82 of the Book “Agile Development with Rails” First  
> > 10: for item in @items  
> > 11: product = item.product  
> > 12: -%\>  
> > 13:   
> > 14: \<%= item.quantity %\>
> > 
> > [CODE]

Debugging here isn’t really needed since we can see that item in this  
case is 1099.0:Float.

Are you sure that your display\_cart method in the controller contains  
@items = @cart.items  
and that your Cart class has been updated to add LineItems to it’s  
@items?

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/same-error-again/100983/7 "2006-08-03T14:23:39Z")

</div>

Once again: Post what your LineItem.for\_product method does! Without  
that  
information one couldn’t tell why your items get populated with Floats.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 3, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/same-error-again/100983/8 "2006-08-03T12:17:25Z")

</div>

On 8/2/06, N/a N/a [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 9: \<%  
> 10: for item in @items  
> 11: product = item.product  
> 12: -%\>  
> 13:   
> 14: \<%= item.quantity %\>
> 
> [CODE]

Ok, so, line 11 is trying to call the method “product” on the object  
“item”. “item” appears to be a Float. There is no such Float#product  
method.

@items appears to be filled with Floats, when it should be filled with  
LineItems. So, you want to check to see where @items is being filled  
with data – the bug is likely there.

Joe
