def add_product(product) current_item = @items.find {|item| item.product == product} if current_item current_item.increment_quantity else @items << CartItem.new(product) end Im constantly getting errors when adding this into my code. Does anyonw know whats wrong with it? Also, can soemone please explain what the second line in the code does: current_item = @items.find {|item| item.product == product} Thanks in advance! -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Kristen, the second line searches for a product within the within the collection of current products. That is @items.find { |item| item.product == product } or you can write it as follows for each item in @items if item.product == product current_item = item break end end Lastly, it seems that you missing an ''end'' of the last line of the method. Good luck, -Conrad On 2/6/07, Kristen <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > def add_product(product) > current_item = @items.find {|item| item.product == product} > if current_item > current_item.increment_quantity > else > @items << CartItem.new(product) > end > > Im constantly getting errors when adding this into my code. Does anyonw > know whats wrong with it? > > Also, can soemone please explain what the second line in the code does: > current_item = @items.find {|item| item.product == product} > > Thanks in advance! > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Hi, the for loop should read... for item in @items # no each within a ruby for loop if item.product == product current_item = item break end end -Conrad On 2/6/07, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Kristen, the second line searches for a product within the within > the collection of current products. That is > > @items.find { |item| item.product == product } > > or you can write it as follows > > for each item in @items > > if item.product == product > current_item = item > break > end > > end > > Lastly, it seems that you missing an ''end'' of the last line of the method. > > Good luck, > > -Conrad > > On 2/6/07, Kristen <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > def add_product(product) > > current_item = @items.find {|item| item.product == product} > > if current_item > > current_item.increment_quantity > > else > > @items << CartItem.new(product) > > end > > > > Im constantly getting errors when adding this into my code. Does anyonw > > know whats wrong with it? > > > > Also, can soemone please explain what the second line in the code does: > > current_item = @items.find {|item| item.product == product} > > > > Thanks in advance! > > > > -- > > 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 -~----------~----~----~----~------~----~------~--~---
> Lastly, it seems that you missing an ''end'' of the last line of the > method.Thanks. That did the trick. I also replaced the second line with the much simpler for loop. Thanks again guys! -- 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 -~----------~----~----~----~------~----~------~--~---