Good day. I am very new to RoR, although not new to web programming and therefore thought I could figure this problem out on my own but to no avail. I am following through the "Agile Web Development With Rails" PDF book and have run into a problem with implementing the ''add to cart'' feature. I have followed along as described in the final release version of the PDF book but when I click on the "Add to Cart" link next to my product, I get the following error: NoMethodError in Store#add_to_cart undefined method `add_product'' for true:TrueClass /app/controllers/store_controller.rb:10:in `add_to_cart'' The framework trace is : /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /base.rb:756:in `send'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /base.rb:756:in `perform_action_without_filters'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /filters.rb:295:in `perform_action_without_benchmark'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /benchmarking.rb:41:in `perform_action_without_rescue'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /benchmarking.rb:41:in `measure'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /benchmarking.rb:41:in `perform_action_without_rescue'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /rescue.rb:80:in `perform_action'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /base.rb:356:in `send'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller /base.rb:356:in `process'' /usr/local/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/dispatcher.rb:32:in `dispatch'' /var/www/razorpicks/pickem/public/dispatch.cgi:10 And the session dump is: --- :cart: true flash: !ruby/hash:ActionController::Flash::FlashHash {} Thanks, M Damon Hill Project Manager IFWORLD, INC. www.ifworld.com <http://www.ifworld.com/> 479.582.5100 (phone) 479.582.5599 (fax) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Can you paste any controller code that gets/sets your ''cart'' variables, either as instances or in session[:cart]? Sounds like you''ve got a typo somewhere. Damon Hill wrote:> Good day. > > I am very new to RoR, although not new to web programming and therefore > thought I could figure this problem out on my own but to no avail. > I am following through the "Agile Web Development With Rails" PDF book > and have run into a problem with implementing the ''add to cart'' feature. > I have followed along as described in the final release version of the > PDF book but when I click on the "Add to Cart" link next to my product, > I get the following error: > > NoMethodError in Store#add_to_cart > > undefined method `add_product'' for true:TrueClass > /app/controllers/store_controller.rb:10:in `add_to_cart''
Thanks for the reply Wilson. This is my app/controllers/store_controller.rb class StoreController < ApplicationController def index @products = Product.saleable_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 session[:cart] ||= Cart.new end end This is my app/models/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 Thanks again, ~d -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Wilson Sent: Wednesday, August 10, 2005 9:21 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Newbie: Problems during Shopping Cart Example Can you paste any controller code that gets/sets your ''cart'' variables, either as instances or in session[:cart]? Sounds like you''ve got a typo somewhere.
what does your cart.rb file in the models directory look like? where add_product is defined M Damon Hill wrote:>Thanks for the reply Wilson. > >This is my app/controllers/store_controller.rb >class StoreController < ApplicationController > > def index > @products = Product.saleable_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 > session[:cart] ||= Cart.new > end > >end > >This is my app/models/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 > >Thanks again, >~d >-----Original Message----- >From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Wilson >Sent: Wednesday, August 10, 2005 9:21 AM >To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >Subject: Re: [Rails] Newbie: Problems during Shopping Cart Example > >Can you paste any controller code that gets/sets your ''cart'' variables, >either as instances or in session[:cart]? Sounds like you''ve got a typo >somewhere. > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
sorry - somehow i missed it =) Francois Paul wrote:> what does your cart.rb file in the models directory look like? > where add_product is defined > > M Damon Hill wrote: > >> Thanks for the reply Wilson. >> >> This is my app/controllers/store_controller.rb >> class StoreController < ApplicationController >> >> def index >> @products = Product.saleable_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 >> session[:cart] ||= Cart.new >> end >> >> end >> >> This is my app/models/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 >> >> Thanks again, >> ~d >> -----Original Message----- >> From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Wilson >> Sent: Wednesday, August 10, 2005 9:21 AM >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: Re: [Rails] Newbie: Problems during Shopping Cart Example >> >> Can you paste any controller code that gets/sets your ''cart'' variables, >> either as instances or in session[:cart]? Sounds like you''ve got a typo >> somewhere. >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> I am following through the "Agile Web Development With Rails" PDF book > and have run into a problem with implementing the ''add to cart'' feature.> NoMethodError in Store#add_to_cart > > undefined method `add_product'' for true:TrueClass > /app/controllers/store_controller.rb:10:in `add_to_cart''It looks like you have a syntax error in your Store controller. Can you post that code here?
Headed to lunch.. sorry for the short reply.. Try taking the extra space out after the ''@cart'' in "@cart = find_cart" I seem to recall having that bite me in the past, but I may just be displaying my ignorance in public here.. M Damon Hill wrote:> Thanks for the reply Wilson. > > This is my app/controllers/store_controller.rb > class StoreController < ApplicationController > > def index > @products = Product.saleable_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 > session[:cart] ||= Cart.new > end > > end > > This is my app/models/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 > > Thanks again, > ~d > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Wilson > Sent: Wednesday, August 10, 2005 9:21 AM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] Newbie: Problems during Shopping Cart Example > > Can you paste any controller code that gets/sets your ''cart'' variables, > either as instances or in session[:cart]? Sounds like you''ve got a typo > somewhere. >
My apologies list. I replied to the wrong thread. Wilson-- WOW. Removing the ''tab'' that I had placed in the source to make code more readable actually made this work. I am not real sure about the existence of a ''tab'' causing the application to not function properly. Is this something that is known throughout Ruby? Is it inherently bad about ignoring ''tab'' characters? Just curious because I like to keep my code as readable as possible but will need to obviously keep this in mind when coding. Thanks again for the help! ~damon Once again, sorry about the cross-thread reply. -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Wilson Sent: Wednesday, August 10, 2005 10:24 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Newbie: Problems during Shopping Cart Example Headed to lunch.. sorry for the short reply.. Try taking the extra space out after the ''@cart'' in "@cart = find_cart" I seem to recall having that bite me in the past, but I may just be displaying my ignorance in public here.. M Damon Hill wrote:> Thanks for the reply Wilson. > > This is my app/controllers/store_controller.rb > class StoreController < ApplicationController > > def index > @products = Product.saleable_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 > session[:cart] ||= Cart.new > end > > end > > This is my app/models/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 > > Thanks again, > ~d > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Wilson > Sent: Wednesday, August 10, 2005 9:21 AM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] Newbie: Problems during Shopping Cart Example > > Can you paste any controller code that gets/sets your ''cart'' > variables, either as instances or in session[:cart]? Sounds like > you''ve got a typo somewhere. >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails