Hi. I''m having a strange error. I''m following a book on Ruby on Rails and it''s guiding me on how to make a shopping cart. I personally have double checked all the codes but I get stuck with the error: NoMethodError in Store#add_to_cart undefined method `<<'' for #<LineItem:0xb6cd2e60> I have add_to_cart: def add_to_cart product = Product.find(params[:id]) @cart = find_cart @cart.add_product(product) redirect_to(:action => ''display_cart'') end And: def add_product(product) item = LineItem.for_product(product) @items << item @total_price += product.price end and it seems to not like @items << item Any help is appreciated. I think I''ve checked the code and I couldn''t find anything wrong... but i''m sure I''ve done something that I can''t catch :( Thanks! -- Posted via http://www.ruby-forum.com/.
I just redid all the code in the chapter and I get the same thing again. It''s as if it doesn''t understand that << means to append or something. Has anyone else had this problem before? -- Posted via http://www.ruby-forum.com/.
You may have a typo. i am looking at hte hbook and it says: @items << LineItem.for_product(product) @total_pice += product.price hope that helps, Mark On 11/25/05, Khurrum Ma <khurrum1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I just redid all the code in the chapter and I get the same thing > again. It''s as if it doesn''t understand that << means to append or > something. > > Has anyone else had this problem before? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -------------------------------------------------------------------- I am Mark Daggett and I approve this message.
Thanks for the reply :) I tried it again with the code from the book that you posted but unfortunately I get the same error :( (sorry I posted the wrong code above earlier. I moved it around just to see if it helps and forgot to change it back) undefined method `<<'' for #<LineItem:0xb6ab7914> So now cart.rb is: 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) # <-- error? @total_price += product.price end end This error is very confusing. :( heavysixer wrote:> You may have a typo. > i am looking at hte hbook and it says: > > @items << LineItem.for_product(product) > @total_pice += product.price > > hope that helps, > Mark-- Posted via http://www.ruby-forum.com/.
Could it have anything to do with the fact that originally the mySQL database was myISAM and not InnoDB? Is there any way to confirm that the contraints are being put on? -- Posted via http://www.ruby-forum.com/.
I think that your problem might be with your line item class not the cart class. I think the error is actually saying it is looking for the "<<" method inside lineItem. I would check the syntax of your lineItem class again. I am using myISAM without troubles. Hope that helps, Mark On 11/25/05, Khurrum Ma <khurrum1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Could it have anything to do with the fact that originally the mySQL > database was myISAM and not InnoDB? > > Is there any way to confirm that the contraints are being put on? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -------------------------------------------------------------------- I am Mark Daggett and I approve this message.
Thanks for the reply :) I just noticed that it was working in IE but not firefox so I deleted the cookies that I had from my domain and it started working :( hehe a day was wasted hehe but I guess I learned something. Thanks for the help :) heavysixer wrote:> I think that your problem might be with your line item class not the > cart class. > I think the error is actually saying it is looking for the "<<" method > inside lineItem. I would check the syntax of your lineItem class > again. I am using myISAM without troubles. > Hope that helps, > Mark > > On 11/25/05, Khurrum Ma <khurrum1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > ---- Posted via http://www.ruby-forum.com/.