Quantcast
Channel: Pragmatic Forums | Posts by osagie uwaifo
Viewing all articles
Browse latest Browse all 8

8.3 Iteration C2: A Smarter Cart posted by osagie uwaifo @ Tue, 20 May 2008 04:21:57 +0000

$
0
0

Hi i can’t get passed this section. I have read and tried everything on the forums. I keep getting this error:

NoMethodError in StoreController#add_to_cart

undefined method `product’ for #<product:0x368b354>

RAILS_ROOT: C:/Rails/rails_apps/depot
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/attribute_methods.rb:205:in `method_missing’
app/models/cart.rb:16:in `add_product’
app/models/cart.rb:16:in `each’
app/models/cart.rb:16:in `find’
app/models/cart.rb:16:in `add_product’
app/controllers/store_controller.rb:19:in `add_to_cart’

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/attribute_methods.rb:205:in `method_missing’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/base.rb:1602:in `find’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `send’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `perform_action_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in `call_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’
c:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in `perform_action_without_caching’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in `perform_action’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/query_cache.rb:8:in `cache’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:677:in `perform_action’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `send’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `process_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in `process_without_session_management_support’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in `process’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in `process’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in `handle_request’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in `dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in `dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/webrick_server.rb:112:in `handle_dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/webrick_server.rb:78:in `service’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run’
c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/webrick_server.rb:62:in `dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/commands/servers/webrick.rb:66
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/active_support/dependencies.rb:496:in `require’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/active_support/dependencies.rb:342:in `new_constants_in’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/active_support/dependencies.rb:496:in `require’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/commands/server.rb:39
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
script/server:3

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/attribute_methods.rb:205:in `method_missing’
app/models/cart.rb:16:in `add_product’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/base.rb:1602:in `find’
app/models/cart.rb:16:in `each’
app/models/cart.rb:16:in `find’
app/models/cart.rb:16:in `add_product’
app/controllers/store_controller.rb:19:in `add_to_cart’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `send’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `perform_action_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in `call_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’
c:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in `perform_action_without_caching’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in `perform_action’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_record/query_cache.rb:8:in `cache’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:677:in `perform_action’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `send’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `process_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in `process_without_session_management_support’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in `process’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in `process’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in `handle_request’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in `dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in `dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/webrick_server.rb:112:in `handle_dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/webrick_server.rb:78:in `service’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run’
c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start’
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/webrick_server.rb:62:in `dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/commands/servers/webrick.rb:66
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/active_support/dependencies.rb:496:in `require’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/active_support/dependencies.rb:342:in `new_constants_in’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2-/lib/active_support/dependencies.rb:496:in `require’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2-/lib/commands/server.rb:39
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
script/server:3

Request

Parameters:

{“authenticity_token”=>”d3f613dc201329a40d6e9cf91e9528e242c96fc9”, “id”=>”1”}

Sorry i copied everything. Basically, it is not finding anything with the CartItem class. The application thinks i am calling the product object. It is not recognizing the cartItem class at all. I have deleted/dropped my database and ran rake db:migrate command again to populate my database again. I am using SQLite3. I am wondering how the cart class is talking to the cartItem class since there is no require ’’ statement in either of the file. And also how the product is actually able to see these files.

This is my controller class
class StoreController < ApplicationController

attr_reader :product

def index
    @products = Product.find_products_for_sale
end
public
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
end
private
def find_cart
    unless session[:cart] # if there's no cart in the session
           session[:cart] = Cart.new # add a new one
    end
           session[:cart] # return existing or new cart
end
end

Basically, I need the application to recognize the CartItem class, if anyone can point me to the right path, i will appreciate it.

I think the error is from this line in the cart class :
current_item = @items.find {|item| item.product == product}

It is returning a product type or a product type object. Basically, cartItem objects and product objects are being stored in the items array.
This code is a little confusing. This line is what is holding me back. I know this becos i put a puts statement after this line and it does not reach my print statement.
Any help would be appreciated.


Viewing all articles
Browse latest Browse all 8

Trending Articles