I’m working my way through the book Agile Web D. with Rails,
and I am in the section where the book has me creating a display_cart
method and the associated view. When I copied the code from the book
into the view, it threw this error. If anyone could give me some hints
where I should be looking I’d appreciate it.
NoMethodError in Store#display_cart
Showing app/views/store/display_cart.rhtml where line #5 raised:
undefined method `product’ for #BigDecimal:47a81c8,‘0.9E3’,4(12)
Extracted source (around line #5):
2:
3: <%
4: for item in @items
5: product = item.product
6: -%>
7:
8: <%= item.quantity %> |
Also here’s my display_cart method:
def display_cart
@cart = find_cart
@items = @cart.items
end
first off don’t use a for loop use each
for @items.each do |item|
product = item.product
end
second items is a BigDecimal object (ie. the number of items in the
cart).
I imagine that cart has a getter named products that you could use
instead.
On Oct 19, 2007, at 12:17 PM, Keynan P. wrote:
instead.
collection.each do |object|
object.stuff
end
is equivalent to
for object in collection
object.stuff
end
Just try having collection be something that doesn’t respond_to
“each” and see the error that the ‘for’ version gives you.
(I know that I’m not really answering the OP’s question about the
cart or the BigDecimal error, but the suggestion in the first
response isn’t valid or even syntactically correct.)
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
Rob B. wrote:
On Oct 19, 2007, at 12:17 PM, Keynan P. wrote:
instead.
collection.each do |object|
object.stuff
end
is equivalent to
for object in collection
object.stuff
end
Just try having collection be something that doesn’t respond_to
“each” and see the error that the ‘for’ version gives you.
(I know that I’m not really answering the OP’s question about the
cart or the BigDecimal error, but the suggestion in the first
response isn’t valid or even syntactically correct.)
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
Is there a quick way to print out the results of @items, would I get
this error if no data was being populated from the db and then I tried
to use it?
I found if I changed the code to this:
Display Cart
<% for item in @items %>
<%= item%>
<% end %>
It printed out just the price field from the database, which makes me
think perhaps something is wrong with my model?
Here’s my table:
id
title
description
image_url
price
date_available
Here’s my 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
Thanks for the help.
Mark
On Oct 19, 2007, at 3:22 PM, Mark Steudel wrote:
I found if I changed the code to this:
Display Cart
> <% for item in @items %>
<% for item in @cart.items %>
> <%= item%>
>
> <% end %>
> price
> @total_price = 0.0
> Mark
> --
Without seeing what you’re doing in the controller, that’s all I can
say.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
On Oct 19, 2007, at 3:15 PM, Mark Steudel wrote:
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
Is there a quick way to print out the results of @items, would I get
this error if no data was being populated from the db and then I tried
to use it?
In a view perhaps?
<% if @items -%>
<% @items.each do |item| -%>
- <%= item %>
<% end -%>
<% else -%>
Sorry, no items
<% end -%>
You could say ‘unless @items.blank?’ in place of ‘if @items’ with the
same result.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
On Oct 19, 2007, at 4:00 PM, Mark Steudel wrote:
class StoreController < ApplicationController
end
end
So does the view work now? You can also have:
<%= item.inspect %>
to find out what item is (if it’s nil, nil.inspect => “nil”)
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
Mark Steudel wrote:
Is there a quick way to print out the results of @items, would I get
this error if no data was being populated from the db and then I tried
to use it?
In your view:
<%= debug(@items) %>
Will give you a nice YAML formatted dump of everything in the object,
including errors and associations.
On Oct 19, 2007, at 6:22 PM, Daniel W. wrote:
Will give you a nice YAML formatted dump of everything in the object,
including errors and associations.
Good point. Using debug() also puts some nice CSS classes on the
output so you can format it (like display:none;) and still get to it
in the “view source” or by changing the CSS dynamically (Firebug or
alternate stylesheets)
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
Daniel W. wrote:
In your view:
<%= debug(@items) %>
Will give you a nice YAML formatted dump of everything in the object,
including errors and associations.
This is what I got when I used debug(@items)
-
!ruby/object:BigDecimal {}
-
!ruby/object:BigDecimal {}
-
!ruby/object:BigDecimal {}
-
!ruby/object:BigDecimal {}
What is the BigDecmial?
Without seeing what you’re doing in the controller, that’s all I can
say.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
Here’s my controller, thanks for the patenience
class StoreController < ApplicationController
def index
@products = Product.salable_items
end
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
def find_cart
session[:cart] ||= Cart.new
end
end
On Oct 20, 2007, at 1:54 AM, Mark Steudel wrote:
What is the BigDecmial?
That’s the class into which database floats and decimal values are
reified. It is a class that retains decimal places accurately
(something that is simply not possible with binary-encoded
approximations to real decimal numbers).
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
So does the view work now? You can also have:
<%= item.inspect %>
to find out what item is (if it’s nil, nil.inspect => “nil”)
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
When I do this, I get this print out:
#