Hi Im going through the rails book tutorial. Using webrick. My gem list --local: rails (0.13.1, 0.11.1) Web-application framework with template engine, control-flow layer, and ORM. Im on os x running ruby 1.8.2 Im at the cart stage... when I click "add to cart" I get this error: NoMethodError in Store#add_to_cart WARNING: You have a nil object when you probably didn''t expect it! Odds are you want an instance of Array instead. Look in the callstack to see where you''re working with an object that could be nil. Investigate your methods and make sure the object is what you expect! /app/models/cart.rb:15:in `add_product'' /app/controllers/store_controller.rb:10:in `add_to_cart'' script/server:49 Show framework trace Request Parameters: {"id"=>"2"} Show session dump Response Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"} Ive deleted the cookie from /tmp and restarted webrick but im still getting this error. I did work for me once... but now it wont... Can anyone help me out? Eoghan
Please ignore! I had a stupid typo! Eoghan On 10 Sep 2005, at 15:33, eoghan wrote:> Hi > Im going through the rails book tutorial. Using webrick. My gem > list --local: > rails (0.13.1, 0.11.1) > Web-application framework with template engine, control-flow > layer, > and ORM. > Im on os x running ruby 1.8.2 > Im at the cart stage... when I click "add to cart" I get this error: > NoMethodError in Store#add_to_cart > > WARNING: You have a nil object when you probably didn''t expect > it! Odds are you > want an instance of Array instead. > > Look in the callstack to see where you''re working with an object > that could be nil. > Investigate your methods and make sure the object is what you expect! > /app/models/cart.rb:15:in `add_product'' > /app/controllers/store_controller.rb:10:in `add_to_cart'' > script/server:49 > Show framework trace > > > Request > > Parameters: {"id"=>"2"} > > Show session dump > > Response > > Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"} > > Ive deleted the cookie from /tmp and restarted webrick but im still > getting this error. I did work for me once... but now it wont... > Can anyone help me out? > Eoghan > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
On 9/10/05, eoghan <rails-JPH02XhdevJeoWH0uzbU5w@public.gmane.org> wrote:> Please ignore! I had a stupid typo! > EoghanBut still, at least 8 people in the past month have posted (I think) that exact same problem to this list. Is the book not obvious enough as to where to put that method?> On 10 Sep 2005, at 15:33, eoghan wrote: > > > Hi > > Im going through the rails book tutorial. Using webrick. My gem > > list --local: > > rails (0.13.1, 0.11.1) > > Web-application framework with template engine, control-flow > > layer, > > and ORM. > > Im on os x running ruby 1.8.2 > > Im at the cart stage... when I click "add to cart" I get this error: > > NoMethodError in Store#add_to_cart > > > > WARNING: You have a nil object when you probably didn''t expect > > it! Odds are you > > want an instance of Array instead. > > > > Look in the callstack to see where you''re working with an object > > that could be nil. > > Investigate your methods and make sure the object is what you expect! > > /app/models/cart.rb:15:in `add_product'' > > /app/controllers/store_controller.rb:10:in `add_to_cart'' > > script/server:49 > > Show framework trace > > > > > > Request > > > > Parameters: {"id"=>"2"} > > > > Show session dump > > > > Response > > > > Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"} > > > > Ive deleted the cookie from /tmp and restarted webrick but im still > > getting this error. I did work for me once... but now it wont... > > Can anyone help me out? > > Eoghan > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 10 Sep 2005, at 16:26, Joe Van Dyk wrote:> But still, at least 8 people in the past month have posted (I think) > that exact same problem to this list. > > Is the book not obvious enough as to where to put that method?yes. it is, now.
I ran into this problem. The reason was that WEBrick only reloaded the cart.rb file when it WEBrick itself was reloaded. So I''d make or change the cart.rb file and then Rails wouldn''t know anything about it and give me a ''nil class'' error. At the same point that Eoghan was getting it, too. So the solution is to simply restart WEBrick and it should work. Note that you seem to have to do this every time you make a change to cart.rb. Is there a better way to do this? cart.rb is a file in the models directory that contains a class, Cart, that is stand-alone (that is, it doesn''t inherit from ActiveRecords or anything else.) Can I force WEBrick to update this file without reloading WEBrick? WEBrick is already running in development mode (via the script that Rails creates) so that isn''t the problem. -Matt> ------------Original Message------------ > From: eoghan <rails-JPH02XhdevJeoWH0uzbU5w@public.gmane.org> > To: joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org, rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Date: Sat, Sep-10-2005 9:20 AM > Subject: Re: [Rails] depot tutorial > > > On 10 Sep 2005, at 16:26, Joe Van Dyk wrote: > > > But still, at least 8 people in the past month have posted (I think) > > that exact same problem to this list. > > > > Is the book not obvious enough as to where to put that method? > > yes. it is, now. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Sep 10, 2005, at 5:51 PM, Magicmat wrote:> I ran into this problem. The reason was that WEBrick only reloaded > the cart.rb file when it WEBrick itself was reloaded. So I''d make > or change the cart.rb file and then Rails wouldn''t know anything > about it and give me a ''nil class'' error. At the same point that > Eoghan was getting it, too. > > So the solution is to simply restart WEBrick and it should work. > Note that you seem to have to do this every time you make a change > to cart.rb. Is there a better way to do this? cart.rb is a file in > the models directory that contains a class, Cart, that is stand- > alone (that is, it doesn''t inherit from ActiveRecords or anything > else.) Can I force WEBrick to update this file without reloading > WEBrick? WEBrick is already running in development mode (via the > script that Rails creates) so that isn''t the problem.It''s a kinda problem in the book, introduced because of a change made in .13.1. The book says that you no longer need the model :cart statement in .13.1, which is true as far as it goes. However, if you leave it out, and don''t note the dependency elsewhere, then Rails doesn''t do the automatic reload on change for non-ActiveRecord models. I think I''m going to add the model :cart line back into the next printing, unless someone suggests a more idiomatic solution. Cheers Dave
On 9/10/05, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote:> > On Sep 10, 2005, at 5:51 PM, Magicmat wrote: > > > I ran into this problem. The reason was that WEBrick only reloaded > > the cart.rb file when it WEBrick itself was reloaded. So I''d make > > or change the cart.rb file and then Rails wouldn''t know anything > > about it and give me a ''nil class'' error. At the same point that > > Eoghan was getting it, too. > > > > So the solution is to simply restart WEBrick and it should work. > > Note that you seem to have to do this every time you make a change > > to cart.rb. Is there a better way to do this? cart.rb is a file in > > the models directory that contains a class, Cart, that is stand- > > alone (that is, it doesn''t inherit from ActiveRecords or anything > > else.) Can I force WEBrick to update this file without reloading > > WEBrick? WEBrick is already running in development mode (via the > > script that Rails creates) so that isn''t the problem. > > > It''s a kinda problem in the book, introduced because of a change made > in .13.1. The book says that you no longer need the model :cart > statement in .13.1, which is true as far as it goes. However, if you > leave it out, and don''t note the dependency elsewhere, then Rails > doesn''t do the automatic reload on change for non-ActiveRecord > models. I think I''m going to add the model :cart line back into the > next printing, unless someone suggests a more idiomatic solution.Adding a "require_dependency ''cart''" in environment.rb would fix it, right?