I am following the Depot tutorial in Agile Web Development with Rail, I am getting the following error, NoMethodError in StoreController#empty_cart undefined method `empty!'' for #<Cart:0x5012250> Here is my call in store_controller.rb, def empty_cart find_cart.empty! flash[:notice] = ''Your cart is now empty'' redirect_to(:action => ''index'') end And in my Model cart.rb, def empty! @items = [] @total_price = 0.0 end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It looks like you might have the empty! method after the private declaration. If so, it will not be visible to other objects. I always name all the private methods with an underscore in the beginning. Just by looking at the code I know which methods are private and will catch this kind of errors quickly. On 11/14/06, Willem Herbst <herbstwc-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > NoMethodError in StoreController#empty_cart > undefined method `empty!'' for #<Cart:0x5012250> > > Here is my call in store_controller.rb, > def empty_cart > find_cart.empty! > flash[:notice] = ''Your cart is now empty'' > redirect_to(:action => ''index'') > end > > And in my Model cart.rb, > > def empty! > @items = [] > @total_price = 0.0 > end > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
well if that is copy/paste code then i got to ask what the hell is find_cart. there is no instantiation -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well here is a direct copy, From store_controller.rb: ===============================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'') rescue logger.error("Attempt to access invalid product #{params[:id]}") redirect_to_index(''Invalid product'') end def display_cart @cart = find_cart @items = @cart.items if @items.empty? redirect_to_index("Your cart is currently empty") end end def empty_cart @cart = find_cart @cart.empty! redirect_to_index(''Your cart is now empty'') end PAGE 97 def checkout @cart = find_cart @items = @cart.items if @items.empty? redirect_to_index("There''s nothing in your cart!") else @order = Order.new end end private def redirect_to_index(msg = nil) flash[:notice] = msg if msg redirect_to(:action => ''index'') end private def find_cart session[:cart] ||= Cart.new end end =====================Then From the model cart.rb: =====================class Cart attr_reader :items attr_reader :total_price def initialize empty! end def empty! @items = [] @total_price = 0.0 end def add_product(product) item = @items.find {|i| i.product_id == product.id} if item item.quantity += 1 else item = LineItem.for_product(product) @items << item end @total_price += product.price end end ================================= --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Never mind this morning when i started up firefox the error just isn''t there anymore, very strange. On 11/15/06, Willem Herbst <herbstwc-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Well here is a direct copy, > > From store_controller.rb: > ===============================> 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'') > rescue > logger.error("Attempt to access invalid product #{params[:id]}") > redirect_to_index(''Invalid product'') > end > > def display_cart > @cart = find_cart > @items = @cart.items > if @items.empty? > redirect_to_index("Your cart is currently empty") > end > end > > def empty_cart > @cart = find_cart > @cart.empty! > redirect_to_index(''Your cart is now empty'') > end > PAGE 97 > def checkout > @cart = find_cart > @items = @cart.items > if @items.empty? > redirect_to_index("There''s nothing in your cart!") > else > @order = Order.new > end > end > > private > def redirect_to_index(msg = nil) > flash[:notice] = msg if msg > redirect_to(:action => ''index'') > end > > private > def find_cart > session[:cart] ||= Cart.new > end > end > > =====================> Then From the model cart.rb: > =====================> class Cart > attr_reader :items > attr_reader :total_price > > def initialize > empty! > end > > def empty! > @items = [] > @total_price = 0.0 > end > > def add_product(product) > item = @items.find {|i| i.product_id == product.id} > > if item > item.quantity += 1 > else > item = LineItem.for_product(product) > @items << item > end > > @total_price += product.price > end > end > =================================>-- Willem Herbst OSS Developer Business Data Solutions Tel #: 021 422 09 51 E-mail: Herbstwc-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---