erix
2006-Jun-23 15:59 UTC
[Rails] Yet another problem with NoMethodError in Store#display_cart
Hi everybody, I''ve search a lot on google to help me with this one without success. I know that some people had the same problem as I have, but there solutions doesn''t seems to work for me. I''m trying to do the depot example in the AWDwR book. But now I''m stock with a problem that I can''t solve. I get this error: <--- begin error here ---> NoMethodError in Store#display_cart Showing app/views/store/display_cart.rhtml where line #4 raised: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.size Extracted source (around line #4): 1: <h1>Display Cart</h1> 2: 3: <p> 4: Your cart contains <%= @items.size %> items. 5: </p> <--- end error here ---> Here is a copy/paste of my code: <--- file store_controller.rb begin here ---> 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 diplay_cart @cart = find_cart @items = @cart.items end private def find_cart session[:cart] ||= Cart.new end end <--- file store_controller.rb end here ---> <--- file application.rb begin here ---> class ApplicationController < ActionController::Base model :cart model :line_item end <--- file application.rb end here ---> <--- file cart.rb begin here ---> 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 <--- file cart.rb end here ---> <--- file line_item.rb begin here ---> class LineItem < ActiveRecord::Base belongs_to :product def self.for_product(product) item = self.new item.quantity = 1 item.product = product item.unit_price = product.price item end end <--- file line_item.rb end here ---> <--- file display_cart.rhtml begin here ---> <h1>Display Cart</h1> <p> Your cart contains <%= @items.size %> items. </p> <--- file display_cart.rhtml end here ---> Here is what I tried so far: - Delete all the cache from my browser (I''m using Opera, but I also tried Firefox) - Delete all the cookies - Check for typos - Restart WEBrick - Reboot my computer (I''m using Windows XP with mySQL 5.0.18 and RoR 1.2) I have put some breakpoint() in my code. The ''Cart'' is created, @items is set correctly and the session is also working (inside store_controller.rb). If I put a breakpoint() method inside the ''display_cart.rhtml'', at the prompt, I can see that the ''session[:cart] variable is properly set, but the ''@items'' is set to ''nil''. It really seems like the ''@items'' is not readable or not set from within the ''display_cart.rhtml'' file. But I think that the '':model cart'' line is correctly set in my ''application.rb''. Can somebody help me with this one? Thanks a lot. -- Posted via http://www.ruby-forum.com/.
cremes.devlist@mac.com
2006-Jun-23 16:11 UTC
[Rails] Yet another problem with NoMethodError in Store#display_cart
On Jun 23, 2006, at 10:58 AM, erix wrote:> Hi everybody, > > I''ve search a lot on google to help me with this one without > success. I > know that some people had the same problem as I have, but there > solutions doesn''t seems to work for me. > > I''m trying to do the depot example in the AWDwR book. But now I''m > stock > with a problem that I can''t solve. I get this error: > > <--- begin error here ---> > NoMethodError in Store#display_cart > > Showing app/views/store/display_cart.rhtml where line #4 raised: > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.size > > Extracted source (around line #4): > > 1: <h1>Display Cart</h1> > 2: > 3: <p> > 4: Your cart contains <%= @items.size %> items. > 5: </p> > <--- end error here ---> >Try changing the #display_cart method to: def diplay_cart @cart = find_cart @items = @cart.items || [] end This will solve the problem when @cart.items returns nil. However, f you previously called #add_to_cart and #display_cart still doesn''t display the items you added, then there''s a bug elsewhere. cr
erix
2006-Jun-23 16:39 UTC
[Rails] Re: Yet another problem with NoMethodError in Store#display_
Chuck Remes wrote:> On Jun 23, 2006, at 10:58 AM, erix wrote: > >> >> >> 1: <h1>Display Cart</h1> >> 2: >> 3: <p> >> 4: Your cart contains <%= @items.size %> items. >> 5: </p> >> <--- end error here ---> >> > > Try changing the #display_cart method to: > > def diplay_cart > @cart = find_cart > @items = @cart.items || [] > end > > This will solve the problem when @cart.items returns nil. However, f > you previously called #add_to_cart and #display_cart still doesn''t > display the items you added, then there''s a bug elsewhere. > > crHi Chuck Remes, I tried your code, but it wasn''t working either. I was still getting the same error. But I finally found the problem: a typo! Look at the name of the display_cart method: ''def diplay_cart'' instead of ''def display_cart''. But you point me on the problem, because it wasn''t logical to get the same error after setting @items to [] if @cart.items was nil. At last, the @items.size was support to return 0. So I looked again in the function and then I saw it. Thanks again! -- Posted via http://www.ruby-forum.com/.
cremes.devlist@mac.com
2006-Jun-23 18:29 UTC
[Rails] Re: Yet another problem with NoMethodError in Store#display_
On Jun 23, 2006, at 11:39 AM, erix wrote:> [snip] > I tried your code, but it wasn''t working either. I was still > getting the > same error. But I finally found the problem: a typo! Look at the > name of > the display_cart method: ''def diplay_cart'' instead of ''def > display_cart''. But you point me on the problem, because it wasn''t > logical to get the same error after setting @items to [] if > @cart.items > was nil. At last, the @items.size was support to return 0. So I looked > again in the function and then I saw it.A typo! My eyes just glanced right over it... Good catch. cr