Help -- NoMethodError in

Hi,

I am just learing Rails and I am going through the Agile Web D.
with Rails book. I am receiving:

NoMethodError in Store#add_to_cart
undefined method `add_product’ for #StoreController:0x3764d80
RAILS_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/controllers/store_controller.rb:12:in add_to_cart' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in
perform_action_without_filters' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:inperform_action_without_benchmark’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:inmeasure’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:inperform_action’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in
send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:inprocess_without_session_management_support’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in
process' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:indispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in
handle_dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:94:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in each' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:instart’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in
dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:inrequire__’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
require__' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
require' ./script/server:3 #{RAILS_ROOT}/app/controllers/store_controller.rb:12:inadd_to_cart’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in
send' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:inperform_action_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in
perform_action_without_benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in
measure' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in
perform_action' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:insend’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in
process_without_session_management_support' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:inprocess’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in
dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:inhandle_dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in
service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' c:/ruby/lib/ruby/1.8/webrick/server.rb:155:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:144:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:89:ineach’
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:79:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in start' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:indispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
require__' c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
require' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28 c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:inrequire__’
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
require' c:/ruby/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: !ruby/hash:ActionController::Flash::FlashHash {}
Response
Headers: {“cookie”=>[], “Cache-Control”=>“no-cache”}

2006/1/11, Chris McDevitt [email protected]:

Hi,

I am just learing Rails and I am going through the Agile Web D.
with Rails book. I am receiving:

NoMethodError in Store#add_to_cart
undefined method `add_product’ for #StoreController:0x3764d80
RAILS_ROOT: ./script/…/config/…

Doeas your StoreController have an add_product method?

If not, you probably need to add it.

If you think you do, please post the relevant code and lets have a
look at it. Also, what does the code in the Store#add_to_cart method
look like?

Douglas

Hi Chris,
This means that your store_controller.rb file is missing a function

def add_to_cart
product = Product.find(params[:id])


end

you might have misspelled the name?

On 1/10/06, Chris McDevitt [email protected] wrote:

#{RAILS_ROOT}/app/controllers/store_controller.rb:12:in add_to_cart' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in dispatch’
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in each' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in perform_action_without_rescue’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in start' require’


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

In theory, the add_product() is located in the model cart…

Here is my store_controler:

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
def find_cart
	# Conditional Assignment Operator
	# In hash session it looks for key work "cart"
	# If found, it returns the current cart
	# If not found a new cart is created and stored in the session hash
	session[:cart] ||= Cart.new
end

end

Teh missing function is located in \app\model\cart.rb:

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

And…
application.rb

Filters added to this controller will be run for all controllers in

the application.

Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

model :Cart
model :Line_item
end

M Daggett wrote:

Hi Chris,
This means that your store_controller.rb file is missing a function

def add_to_cart
product = Product.find(params[:id])


end

you might have misspelled the name?

On 1/10/06, Chris McDevitt [email protected] wrote:

#{RAILS_ROOT}/app/controllers/store_controller.rb:12:in add_to_cart' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in dispatch’
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in each' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in perform_action_without_rescue’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in start' require’


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

This line:

@cart = add_product(product)

Should be

@cart.add_product(product)

def add_to_cart
	product = Product.find(params[:id])
	@cart = find_cart
	@cart.add_product(product)
	redirect_to(:action => 'display_cart')
end

Douglas L. wrote:

2006/1/11, Chris McDevitt [email protected]:

Hi,

I am just learing Rails and I am going through the Agile Web D.
with Rails book. I am receiving:
Douglas

Any Sugestions?

Tom F. wrote:

This line:

@cart = add_product(product)

Should be

@cart.add_product(product)

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end

That worked! Thanks very very much!