Troubleshooting Depot App


#1

Hello! I am working my way through the Agile Web D. guide on
RoR (the depot application).

I am a bit lost as to where to debug the following error. The error is
pasted below:

================================================================================================
NameError in Store#add_to_cart

undefined local variable or method `product’ for LineItem:Class

RAILS_ROOT: script/…/config/…
Application Trace | Framework Trace | Full Trace

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:991:in
method_missing' #{RAILS_ROOT}/app/models/line_item.rb:7:infor_product’
#{RAILS_ROOT}/app/models/cart.rb:11:in add_product' ./script/../config/../app/controllers/store_controller.rb:10:inadd_to_cart’

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:991:in
method_missing' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:inperform_action_without_filters’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in
perform_action_without_benchmark' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
/usr/local/lib/ruby/1.8/benchmark.rb:293:in measure' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in
perform_action' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:inprocess_without_session_management_support’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in
process' /usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:indispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in
handle_dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
/usr/local/lib/ruby/1.8/webrick/server.rb:173:in start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:23:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:82:instart’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in
dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
require' /usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
`require’
script/server:3

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:991:in
method_missing' #{RAILS_ROOT}/app/models/line_item.rb:7:infor_product’
#{RAILS_ROOT}/app/models/cart.rb:11:in add_product' ./script/../config/../app/controllers/store_controller.rb:10:inadd_to_cart’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in
perform_action_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:inperform_action_without_benchmark’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/local/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:inperform_action’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in
process_without_session_management_support' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:inprocess’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in
dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:inhandle_dispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in
service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:95:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in start' /usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:indispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/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.2.5/lib/active_support/dependencies.rb:214:inrequire’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28
/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.2.5/lib/active_support/dependencies.rb:214:inrequire’
script/server:3

Request

Parameters: {“id”=>“2”}

Show session dump


:cart: !ruby/object:Cart
items: []

total_price: 0.0
flash: !map:ActionController::Flash::FlashHash {}

Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

================================================================================================

So I am assuming that there is a problem with the variable ‘product’,
but I am having trouble finding it. I don’t want to bombard you with
code, but any direction would be helpful.


#2

Update…

It seems the error is being found in this code:

class Cart

    attr_reader :items
    attr_reader :total_price

    def initialize
            @items = []
            @total_price = 0.0
    end
    def add_product(product)
            @items << LineItem.for_product(product)
            @total_price += product.price
    end

end

I don’t see why it is saying product is undefined. Any ideas?


#3

Ok…found it. Pluralized product in the wrong place.

Darn typos.


#4

You may also find this http://books.pragprog.com/titles/rails/errata
helpful. It’s an errata for the book and it helps me very much.

Leo