OK im stuck again... I am following the agile web development with rails book and after i made the cart i get an error: undefined method `sum'' for #<Array:0x8ca5e24> the code i have in cart.rb is: def total_price @items.sum { |item| item.price } end and in view I have: <div class="cart-title">Your Cart</div> <table> <% for cart_item in @cart.items %> <tr> <td><%= cart_item.quantity %>×</td> <td><%= h(cart_item.title) %></td> <td class="item-price"><%= number_to_currency(cart_item.price) %></td> </tr> <% end %> <tr class="total-line"> <td colspan="2">Total</td> <td class="total-cell"><%= number_to_currency(@cart.total_price) %></td> </tr> </table> <%= button_to "Empty cart", :action => :empty_cart %> -- 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 -~----------~----~----~----~------~----~------~--~---
Eric wrote:> OK im stuck again... I am following the agile web development with rails > book and after i made the cart i get an error: > > undefined method `sum'' for #<Array:0x8ca5e24> > > the code i have in cart.rb is: > def total_price > @items.sum { |item| item.price } > endthe view is not important. the idea is that sum is not a known method for an array. by dropping the ''sum'' method and doing this action yourself in the total_price action like: def total_price total=0 @items.each {|item| total += item.price } total end you should be getting the same effect. you may like to define ''sum'' in the Array Class, but that may be too much for the given task, so i believe the above should be enough. hope it helps, somewhat. harp -- 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 -~----------~----~----~----~------~----~------~--~---
From the most recent update to the Agile2 PDF: "I need to stress that the book is written on the assumption that the reader has Rails 1.2 installed. As that doesn''t yet exist, you HAVE TO HAVE EDGE RAILS for some of this stuff to work." - Dave Thomas This cart/sum thing is coming up a lot the last couple of days. The good thing is that instead of just copying lines out of a book, the tutorial makes you figure out alternate ways of doing things when they don''t work. The bad thing is there are a lot of people just starting out, getting really frustrated. --f On 10/8/06, harper <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Eric wrote: > > OK im stuck again... I am following the agile web development with rails > > book and after i made the cart i get an error: > > > > undefined method `sum'' for #<Array:0x8ca5e24> > > > > the code i have in cart.rb is: > > def total_price > > @items.sum { |item| item.price } > > end > > the view is not important. the idea is that sum is not a known method > for an array. by dropping the ''sum'' method and doing this action > yourself in the total_price action like: > > def total_price > total=0 > @items.each {|item| total += item.price } > total > end > > > you should be getting the same effect. > you may like to define ''sum'' in the Array Class, but that may be too > much for the given task, so i believe the above should be enough. > hope it helps, somewhat. > > harp > > -- > 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 -~----------~----~----~----~------~----~------~--~---