How do I fix this? Power went down.

I was successful in reaching page 184 in the book "Agile Web Development with Rails" 3rd edition and while running the website with PostGreSQL as the backend, the power went down. The PostGreSQL database contents look OK and so do the source code files I was editing. But whenever I click on any "Add To Cart" button in the catalog, I get the following information in the terminal window where the "script/server" command was issued. By the way, I'm using Mongrel, not Webrick.

Processing StoreController#add_to_cart (for at 2009-04-06 11:49:15) [POST]   Parameters: {"commit"=>"Add to Cart", "authenticity_token"=>"2hiFEqPkmMUXAZnTJ6jN4TKkMagLt0BY0W/M6fJj1+w=", "id"=>"1"}   Product Load (1.0ms) SELECT * FROM "products" WHERE ("products"."id" = 1)

NoMethodError (You have a nil object when you didn't expect it! The error occurred while evaluating nil.add_product):   app/controllers/store_controller.rb:9:in `add_to_cart'   /usr/lib/ruby/1.8/mongrel.rb:159:in `process_client'   /usr/lib/ruby/1.8/mongrel.rb:158:in `each'   /usr/lib/ruby/1.8/mongrel.rb:158:in `process_client'   /usr/lib/ruby/1.8/mongrel.rb:285:in `run'   /usr/lib/ruby/1.8/mongrel.rb:285:in `initialize'   /usr/lib/ruby/1.8/mongrel.rb:285:in `new'   /usr/lib/ruby/1.8/mongrel.rb:285:in `run'   /usr/lib/ruby/1.8/mongrel.rb:268:in `initialize'   /usr/lib/ruby/1.8/mongrel.rb:268:in `new'   /usr/lib/ruby/1.8/mongrel.rb:268:in `run'

Rendered rescues/_trace (72.4ms) Rendered rescues/_request_and_response (0.8ms) Rendering rescues/layout (internal_server_error)

I really don't want to lose all my work, but I'm totally lost on how to fix this problem.



If you cannot see why the object at line 9 in store_controller.rb is nil then now is your chance to learn about ruby-debug. Have a look and see what is happening. (I don’t know the answer).

Nice idea, but I don't have any books or tutorials on that...don't even know how to start the debugger.


OK, got ruby-debug installed and issued the command: script/server -u Got running in debug mode. No more info than before. Now what?


There are lots of tutorials. Also look at the rails guides and railscasts. I can’t point to them off the top of my head but I am sure google will find them.

Does this line produce a product?

Product Load (1.0ms) SELECT * FROM "products" WHERE ("products"."id" = 1)

Obviously not in the estimation of the mongrel server and interacting code, but the database records are there and the id does exist.


I tried dropping ALL the tables in the PostGreSQL database and running rake db:migrate again. This results in the very same error messages as before. As the system was running successfully before the power outage, I'm of the opinion that there is something else going on here...perhaps there's a way to recompile all of the ruby source files?


You need to show your controller or view.

I seriously doubt something went that wrong with ruby, but an apt-get should work just fine to re-install.


Lets have a look at your store controller...

The nil happens here app/controllers/store_controller.rb:9:in `add_to_cart'