Hi I''m very keen to learn RoR and have been following the Agile Web Development with Rails book (first edition). However, I''ve come across a problem which has had me stumped for days now. After following the instructions up to page 85 I have tried testing the code thus far, as suggested, by adding an item to the cart. But I am getting the following error: -- NoMethodError in StoreController#add_to_cart You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.<< -- I''ve been testing in Firefox to ensure that the cookie is being created (it appears to be OK). Does anyone know what I''m missing/ doing wrong? Also, this may or may not help... if I try to start the lighttpd server I get nothing working and have to resort to using Webrick. So it may be a installation fault and not a coding error. Thanks in advance, Steve -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2413 bytes Desc: not available Url : wrath.rubyonrails.org/pipermail/rails/attachments/20060418/ceb4d3fb/smime.bin
Alan Francis
2006-Apr-18 13:00 UTC
[Rails] Re: ''depot'' tutorial failing on adding to cart
Hi Steve, can you post the contents of your add_to_cart method and the rest of the stack trace? Alan Steve Savery wrote:> -- > NoMethodError in StoreController#add_to_cart > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.<< > ---- Posted via ruby-forum.com.
Steve Savery
2006-Apr-18 13:36 UTC
[Rails] Re: ''depot'' tutorial failing on adding to cart
Hi Alan Below is the Full Trace (sorry - still new, so not sure if this is what you require) and the add_to_cart method. **Full Trace** #{RAILS_ROOT}/app/models/Cart.rb:12:in `add_product'' #{RAILS_ROOT}/app/controllers/store_controller.rb:10:in `add_to_cart'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/ action_controller/base.rb:910:in `perform_action_without_filters'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/ action_controller/filters.rb:368:in `perform_action_without_benchmark'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/ action_controller/benchmarking.rb:69:in `perform_action_without_rescue'' /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/ action_controller/benchmarking.rb:69:in `perform_action_without_rescue'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/ action_controller/rescue.rb:82:in `perform_action'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/ action_controller/base.rb:381:in `process_without_filters'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/ action_controller/filters.rb:377:in `process_without_session_management_support'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/ action_controller/session_management.rb:117:in `process'' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb: 115:in `handle_dispatch'' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb: 81:in `service'' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'' /usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'' /usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'' /usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start'' /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'' /usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'' /usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb: 67:in `dispatch'' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/ webrick.rb:59 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/ active_support/dependencies.rb:147:in `require'' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/ active_support/dependencies.rb:147:in `require'' script/server:3 **End Full Trace** **store_controller.rb** class StoreController < ApplicationController def index @products = Product.salable_items end def add_to_cart product = Product.find(params[:id]) @cart = find_cart @cart.add_product(product) redirect_to(:action => ''display_cart'') end def display_cart @cart = find_cart @items = @cart.items end private # Return a cart object. If we already have one cached # in the session, use it, otherwise create a new one # and add it to the session def find_cart session[:cart] ||= Cart.new end end **End store_controller.rb** Thanks for any help. S On Apr 18, 2006, at 2:11 pm, rails-request@lists.rubyonrails.org wrote:> Hi Steve, > > can you post the contents of your add_to_cart method and the rest > of the > stack trace? > > Alan-------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2413 bytes Desc: not available Url : wrath.rubyonrails.org/pipermail/rails/attachments/20060418/e47598d1/smime.bin
Steve Savery
2006-Apr-18 13:45 UTC
[Rails] Re: ''depot'' tutorial failing on adding to cart
Hi Alan I''ve just cleared my Cache again (thought I''d done this a dozen times this morning alone) and cleared the cookies (again!). Low and behold it is now working - no idea why it should be different this time around tho? Perhaps I was too quick between refreshes for Webrick or something?? Thanks for your help. S On Apr 18, 2006, at 2:11 pm, rails-request@lists.rubyonrails.org wrote:> Hi Steve, > > can you post the contents of your add_to_cart method and the rest > of the > stack trace? > > Alan-------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2413 bytes Desc: not available Url : wrath.rubyonrails.org/pipermail/rails/attachments/20060418/461c9b44/smime.bin
Steve, Could you also post your cart.rb, especially the add_product:>**Full Trace** >#{RAILS_ROOT}/app/models/Cart.rb:12:in `add_product'' >#{RAILS_ROOT}/app/controllers/store_controller.rb:10:in `add_to_cart''I also want to see how your attribute reader is set up in cart.rb, as this is not a normal generated model. -Sean Alan Francis wrote:> Hi Steve, > > can you post the contents of your add_to_cart method and the rest of the > stack trace? > > Alan > > Steve Savery wrote: >> -- >> NoMethodError in StoreController#add_to_cart >> >> You have a nil object when you didn''t expect it! >> You might have expected an instance of Array. >> The error occured while evaluating nil.<< >> ---- Posted via ruby-forum.com.