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: <table>
9: <%
10: for item in @items
11: product = item.product
12: -%>
13: <tr>
14: <td><%= item.quantity %></td>
[CODE]
My display_cart.rhtml from views/store/ is as follows:
[CODE]
<!--
! Excerpted from "Agile Web Development with Rails"
! We make no guarantees that this code is fit for any purpose.
! Visit http://www.pragmaticprogrammer.com for more book information.
-->
<h1>Display Cart</h1>
<table>
<%
for item in @items
product = item.product
-%>
<tr>
<td><%= item.quantity %></td>
<td><%= h(product.title) %></td>
<td align="right"><%= item.unit_price %></td>
<td align="right"><%= item.unit_price * item.quantity
%></td>
</tr>
<% end -%>
</table>
[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.
--
Posted via http://www.ruby-forum.com/.
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060802/390fd997/attachment.html
On 8/2/06, N/a N/a <cain_152@yahoo.com> 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: <table> > 9: <% > 10: for item in @items > 11: product = item.product > 12: -%> > 13: <tr> > 14: <td><%= item.quantity %></td> > [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
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: <table> > 9: <% > 10: for item in @items > 11: product = item.product > 12: -%> > 13: <tr> > 14: <td><%= item.quantity %></td> > [CODE]-- Posted via http://www.ruby-forum.com/.
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: <table> >> 9: <% >> 10: for item in @items >> 11: product = item.product >> 12: -%> >> 13: <tr> >> 14: <td><%= item.quantity %></td> >> [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 Bini (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.
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: <table> >> 9: <% >> 10: for item in @items >> 11: product = item.product >> 12: -%> >> 13: <tr> >> 14: <td><%= item.quantity %></td> >> [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>". -- Posted via http://www.ruby-forum.com/.
Can anyone help? I really reached a deadline here. -- Posted via http://www.ruby-forum.com/.
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/3/06, N/a N/a <cain_152@yahoo.com> wrote:> > Can anyone help? I really reached a deadline here. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060803/2fab4357/attachment.html