Same error ... again

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.

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

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 %>

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

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 %>

Can anyone help? I really reached a deadline here.

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

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)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)

“Yields falsehood when quined” yields falsehood when quined.

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

On 8/2/06, N/a N/a [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs