Forum: Ruby on Rails Problem with the Depot program..

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jason M. (Guest)
on 2007-02-20 04:01
Reading the great book agile web development with rails and I am running
into a problem around the pages 100.

Getting this error, I have tried rake tmp:sessions:clear and rake
db:sessions:clear but still get the the same problem.

Attached is the error message, when I try and add something to the cart
with the full trace and my code.

Thank you!

ERROR MESSAGE:
[code=]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 occurred while evaluating nil.find
RAILS_ROOT: script/../config/..

Application Trace | Framework Trace | Full Trace
app/models/cart.rb:9:in `add_product'
app/controllers/store_controller.rb:10:in `add_to_cart'
Request

Parameters: {"id"=>"3"}

Show session dump

Response

Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}[/code]
FULL TRACE
[code=]app/models/cart.rb:9:in `add_product'
app/controllers/store_controller.rb:10:in `add_to_cart'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:in
`send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:in
`perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:632:in
`call_filter'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:619:in
`perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66: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.13.2/lib/action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/rescue.rb:83:in
`perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in
`send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in
`process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:624:in
`process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/session_management.rb:114:in
`process'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:330:in
`process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/dispatcher.rb:41:in
`dispatch'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:78:in
`process'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in
`synchronize'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in
`process'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:618:in
`process_client'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in
`each'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in
`process_client'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in
`run'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in
`initialize'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in
`new'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in
`run'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in
`initialize'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in
`new'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in
`run'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:271:in
`run'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in
`each'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:270:in
`run'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:127:in
`run'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/command.rb:211:in
`run'
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:243
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in
`load'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in
`load'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in
`new_constants_in'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in
`load'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/servers/mongrel.rb:60
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:in
`require'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in
`new_constants_in'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:in
`require'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/server.rb:39
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require'
script/server:3[/code]
cart.RB
[code=]class Cart
  attr_reader :items

  def intialize
    @items = []
  end

  def add_product(product)
     current_item = @items.find {|item| item.product == product}
     if current_item
       current_item.increment_quantity
     else
       @items <<  CartItem.new(product)
     end
  end
end[/code]
store_controller.rb
[code=]class StoreController < ApplicationController

  def index
    @products = Product.find_products_for_sale
  end

  def add_to_cart
    @cart = find_cart
    product = Product.find(params[:id])
    @cart.add_product(product)
  end

  private

  def find_cart
    session[:cart] ||= Cart.new
  end

end[/code]
cart_item.rb
[code=]class CartItem

  attr_reader :product, :quantity

  def intialize(product)
    @product = product
    @quantitiy = 1
  end

  def increment_quantity
    @quantity += 1
  end

  def title
    @product.title
  end

  def price
    @product.price = @quantity
  end

end[/code]
Eric (Guest)
on 2007-02-20 05:46
(Received via mailing list)
I'm not positive but I think I ran into the same problem and had to
restart my server to fix it.
This topic is locked and can not be replied to.