small medium large xlarge

Xit_pragsmall
20 May 2008, 05:53
osagie uwaifo (8 posts)

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 #

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.

Samr_small_pragsmall
20 May 2008, 10:52
Sam Ruby (634 posts)

The error you are describing sounds suspiciously like the error described on page 111 of B1.1. The solution described there is to issue the command:

rake db:sessions:clear

If you are still seeing product objects in your @items array, I would like to see your add_product method implementation from “app/models/cart.rb”:http://media.pragprog.com/titles/rails3/code/depot_g/app/models/cart.rb.

Xit_pragsmall
20 May 2008, 15:17
osagie uwaifo (8 posts)

Thank you for your quick response. I will try that and see what happens.

Xit_pragsmall
20 May 2008, 18:16
osagie uwaifo (8 posts)

This is my Cart class

class Cart

attr_reader :items , :quantity# <whatis linkend="wtf.attr.accessor">attr_reader</whatis...*TRUNC*

def initialize
    @items = []
end


def add_product(product)
    
    
    @items << product
    puts @items
    
    current_item = @items.find {|item| item.product == product}
    puts "printing current_item " +current_item
    if current_item
        current_item.increment_quantity
    else
       
        @items << CartItem.new(product)
    end
end


def total_price
    @items.sum { |item| item.price }
end

end

Xit_pragsmall
20 May 2008, 18:25
osagie uwaifo (8 posts)

It is still creating product object. And actually seems right to me. when the add product method is called, we are passing in a product object. The we add the product object to the @items array.

current_item = @items.find { item item.product == product}

From the line above we do try to call a method item.product that really does not exist. This seems right.

I think there should be a better way of writing this method. Please let me know what you think. Thanks for your time.

Samr_small_pragsmall
20 May 2008, 20:57
Sam Ruby (634 posts)

It looks to me like your add_produce method does nothing… it immediately is followed by an end instead of actually containing the following code. The “app/models/cart.rb”:http://media.pragprog.com/titles/rails3/code/depot_i/app/models/cart.rb is supposed to look something like this:

class Cart
  attr_reader :items
  
  def initialize
    @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
  
  def total_price
    @items.sum { |item| item.price }
  end
  
end
Xit_pragsmall
20 May 2008, 21:34
osagie uwaifo (8 posts)

Thanks a lot. I will try the code above and let you know if it works.

Xit_pragsmall
21 May 2008, 01:48
osagie uwaifo (8 posts)

I just wanted to say thanks Sam for your guidance. It worked just fine. I really don’t know were i got this line from :@items « product. Thanks a lot.

You must be logged in to comment