small medium large xlarge

Generic-user-small
14 May 2009, 17:23
Dennis Keith (13 posts)

I have been crusing through the book and was at the end of the I18n section when I thought I’d go back and test the functionality of the store. It appears that, somewhere in my code I am either creating a new Cart or Items at the end of a add_to_cart because I can no longer go to checkout nor add any additional items. What is the best approach to finding my bug? In other code, I have always had an IDE that allowed me to step through but here I cannot. I have added a logger.error in add_to_cart so I know I am getting that far.

Any suggestions would be appreciated.

Thanks

Dennis Keith

Chuck_buddy_25_pragsmall
14 May 2009, 21:14
Charles Eckenroed (5 posts)

You have a couple of options:

  1. Add your own trace messages. For example: Put logger.info(“== checkout called. @cart = “ + @cart.to_s)

The output will show in the terminal running the webserver and it is also saved in the log file.

  1. Add “debugger” to stop the program and then step through the code with “n”. You can display values by simply typing their name in terminal while it is stopped.

for #2 above you’ll need to start your webserver with debugging turned on. e.g. ./script/server –debugger

Generic-user-small
15 May 2009, 17:26
Dennis Keith (13 posts)

Charles,

Thanks for your help.I was unable to implement the second option - system couldn’t find ‘fork’ - so I went with 1. I had to add a dependence on applicationm to the cart class so I could use the logger functionality. The following shows the log with two successive calls to add_product. Note that in the first everything looks fine but in the second, where I would expect to see a length of the items array of 1, it is 0. Is there possibly a scoping issue here? I am not familiar with scoping constraints in Ruby.

Processing StoreController#add_to_cart (for 127.0.0.1 at 2009-05-15 10:15:33) [POST] Parameters: {“commit”=>”Add to Cart”, “authenticity_token”=>”bPZSMYHQLhBfcirS+bKfHgC+530KablP7Cs6nV+tBBw=”, “id”=>”2”} Top of add_to_cart Add_to_cart cart.items.length 0 Product Load (0.0ms) SELECT * FROM “products” WHERE (“products”.”id” = 2)  Add_to_cart product to add # Add_to_cart cart.items.length 0 Top of add_product Add_product length of cart.items 0 Add_product product to add # Add_product current_item already in items Add_Product product to add # Add_product length of cart.items 1 Add_to_cart cart.items.length after cart,add_product 1 Add_to_cart cart.items.length after format 1 Rendering store/add_to_cart Rendered store/_cart_item (0.0ms) Rendered store/_cart (16.0ms) Completed in 78ms (View: 16, DB: 0) | 200 OK [http://localhost/store/add_to_cart/2]

Processing StoreController#add_to_cart (for 127.0.0.1 at 2009-05-15 10:15:38) [POST] Parameters: {“commit”=>”Add to Cart”, “authenticity_token”=>”bPZSMYHQLhBfcirS+bKfHgC+530KablP7Cs6nV+tBBw=”, “id”=>”2”} Top of add_to_cart Add_to_cart cart.items.length 0 /* I would have expected this to be 1 */ Product Load (0.0ms) SELECT * FROM “products” WHERE (“products”.”id” = 2)  Add_to_cart product to add # Add_to_cart cart.items.length 0 Top of add_product Add_product length of cart.items 0 Add_product product to add # Add_product current_item already in items Add_Product product to add # Add_product length of cart.items 1 Add_to_cart cart.items.length after cart,add_product 1 Add_to_cart cart.items.length after format 1 Rendering store/add_to_cart Rendered store/_cart_item (16.0ms) Rendered store/_cart (16.0ms) Completed in 78ms (View: 32, DB: 0) | 200 OK [http://localhost/store/add_to_cart/2]

Puzzled

Dennis Keith

Generic-user-small
15 May 2009, 19:46
Dennis Keith (13 posts)

Charles,

FWIW, I get the following response when I try option 2.

C:\InstantRails\rails_apps\depot>ruby script/server webrick -debug => Booting WEBrick => Rails 2.3.2 application starting on http://0.0.0.0:3000 C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/process/daemon.rb:4:in fork': fork() function is unimplemented on this machine (NotImplementedError) from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport- 2.3.2/lib/active_support/core_ext/process/daemon.rb:4:in daemon’ from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails- 2.3.2/lib/commands/server.rb:62 from C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/ custom_require.rb:31:in gem_original_require' from C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/ custom_require.rb:31:in require’ from script/server:3

Any ideas?

Thanks

Dennis Keith

Chuck_buddy_25_pragsmall
15 May 2009, 22:03
Charles Eckenroed (5 posts)

C:\InstantRails\rails_apps\depot>ruby script/server webrick -debug

Try taking out the “webrick” and run it like this:

ruby script/server --debugger

btw, I normally use osx and linux. So I fired up vista, installed ruby, rails and the necessary gems (including ruby-debug) and created a simple example website using scaffolding. I tested the above and and it worked fine for me.

What OS are you using?

Chuck_buddy_25_pragsmall
15 May 2009, 21:58
Charles Eckenroed (5 posts)

Also make sure you use two hyphens in front of the “debug” and not a single one. See my example call above.

Generic-user-small
15 May 2009, 22:03
Dennis Keith (13 posts)

I tried that with the response:

C:\InstantRails\rails_apps\depot>ruby script/server -debug => Booting Mongrel => Rails 2.3.2 application starting on http://0.0.0.0:3000 C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/process/daemon.rb:4:in fork': fork() function is unimplemented on this machine (NotImplementedError) from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/process/daemon.rb:4:in daemon’ from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:62 from C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require' from C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in require’

I have specified WEBrick since the book was written against this server. My operating system is Windows XP Pro, SP3. What bothers me is that I feel certain that if I simply replace the code I have written with that supplied by the authors, it will work and I will never know what stupid thing I did.

Dennis Keith

Generic-user-small
15 May 2009, 22:08
Dennis Keith (13 posts)

Charles,

Our messages must have crossed. I tried –debug and that works - after I installed ruby-debug….

I will walk the code this evening and see if I can find out where the cart.items is being stripped or a new cart is inavertently created.

Thanks

Dennis Keith

Generic-user-small
17 May 2009, 01:13
Dennis Keith (13 posts)

Charles,

Thanks for your help. I found my error using the debugger. A case of a missing ‘=’. Sorry it took so long.

Dennis Keith

Chuck_buddy_25_pragsmall
17 May 2009, 04:04
Charles Eckenroed (5 posts)

Dennis,

Your welcome. Glad you found it.

-chuck

You must be logged in to comment