I''m working my way through the book Agile Web Development 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: <table> 3: <% 4: for item in @items 5: product = item.product 6: -%> 7: <tr> 8: <td><%= item.quantity %></td> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Also here''s my display_cart method: def display_cart @cart = find_cart @items = @cart.items end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 19, 2007, at 12:17 PM, Keynan Pratt wrote:> 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. > --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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> On Oct 19, 2007, at 12:17 PM, Keynan Pratt 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 Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgIs 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I found if I changed the code to this: <h1>Display Cart</h1> <table> <% for item in @items %> <%= item%> <% end %> </table> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 19, 2007, at 3:15 PM, Mark Steudel wrote:> Rob Biedenharn wrote: >> On Oct 19, 2007, at 12:17 PM, Keynan Pratt 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 Biedenharn http://agileconsultingllc.com >> Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > 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 -%> <ul> <% @items.each do |item| -%> <li><%= item %></li> <% end -%> </ul> <% else -%> <p>Sorry, no items</p> <% end -%> You could say ''unless @items.blank?'' in place of ''if @items'' with the same result. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 19, 2007, at 3:22 PM, Mark Steudel wrote:> > I found if I changed the code to this: > > <h1>Display Cart</h1> > <table><thead> <tr><th>Title</th></tr> </thead> <tbody>> <% for item in @items %><% for item in @cart.items %>> <%= item%><tr><td><%= item.title %></td></tr>> > <% end %></tbody>> </table> > > 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 > --Without seeing what you''re doing in the controller, that''s all I can say. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Without seeing what you''re doing in the controller, that''s all I can > say. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgHere''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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 19, 2007, at 4:00 PM, Mark Steudel wrote:>> Without seeing what you''re doing in the controller, that''s all I can >> say. >> >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > 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 > --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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 19, 2007, at 6:22 PM, Daniel Waite wrote:> 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.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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgWhen I do this, I get this print out: # # # -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Waite 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 20, 2007, at 1:54 AM, Mark Steudel wrote:> Daniel Waite 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?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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---