Forum: Ruby on Rails Help -- NoMethodError in

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.
6ddf245deb639ae9eab2e71165af5e98?d=identicon&s=25 Chris Mcdevitt (xlander)
on 2006-01-11 01:03
Hi,

I am just learing Rails and I am going through the Agile Web Development
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:in
`send'
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:in
`perform_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:in
`measure'
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:in
`perform_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: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:in
`process'
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:in
`handle_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:in `service'
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'
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: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:in
`require__'
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: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:in
`require__'
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:in
`require'
./script/server:3
#{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:in
`send'
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:in
`perform_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:in
`measure'
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:in
`perform_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: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:in
`process'
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:in
`handle_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:in `service'
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
c:/ruby/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'
c:/ruby/lib/ruby/1.8/webrick/server.rb:94:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'
c:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'
c:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'
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: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:in
`require__'
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: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:in
`require__'
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:in
`require'
./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"}
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2006-01-11 01:20
(Received via mailing list)
2006/1/11, Chris McDevitt <jcmcdevitt@yahoo.com>:
> Hi,
>
> I am just learing Rails and I am going through the Agile Web Development
> 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
51f10739fd903d3b57561a15e4598954?d=identicon&s=25 M Daggett (Guest)
on 2006-01-11 01:23
(Received via mailing list)
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 <jcmcdevitt@yahoo.com> 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
6ddf245deb639ae9eab2e71165af5e98?d=identicon&s=25 Chris Mcdevitt (xlander)
on 2006-01-11 01:34
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 <jcmcdevitt@yahoo.com> 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
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
> --
6ddf245deb639ae9eab2e71165af5e98?d=identicon&s=25 Chris Mcdevitt (xlander)
on 2006-01-11 02:36
Douglas Livingstone wrote:
> 2006/1/11, Chris McDevitt <jcmcdevitt@yahoo.com>:
>> Hi,
>>
>> I am just learing Rails and I am going through the Agile Web Development
>> with Rails book.  I am receiving:
> Douglas

Any Sugestions?
3f900b38ec3b2c45427c354722fa4ce3?d=identicon&s=25 Tom Fakes (tomfakes)
on 2006-01-11 02:42
(Received via mailing list)
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
6ddf245deb639ae9eab2e71165af5e98?d=identicon&s=25 Chris Mcdevitt (xlander)
on 2006-01-11 03:33
Tom Fakes 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!
This topic is locked and can not be replied to.