Re: undefined method `items'' for nil:NilClass

Michael Pavling
Wed, 20 Oct 2010 08:38:28 -0000

The important line to start at is where the error says:
> Showing app/views/store/_cart.html.erb where line #3 raised:
> undefined method `items'' for nil:NilClass

So you know that on line 3 of that file, you''re accessing the .items
method of an object, but that object is actually nil... when you
probably expect it to be an instance of something.

Line 3 says:
> 3:   <%= render(:partial => "cart_item", :collection =>
cart.items) %>

so we''re looking at the "cart.items" - and can safely assume
that
"cart" isn''t actually a Cart object (or whatever it needs to
be), but
is in fact nil. Since this is a partial - you probably have a line in
your index.html.erb file which renders the partial, passing a Cart to
it.

Looking at the controller''s index method (that''s where you
said it was
failing), we can see that there''s no definition for the
@cart:
>  def index
>    @cameras = Camera.find_cameras_for_sale
>    @cameratypes = Camera.find_cameras_by_camtype
>  end

So you might benefit from putting a line "@cart = find_cart" in there.
On the flip side (because there''s a couple of gaps in the code you
posted), it may be that "cart" in the partial actually needs to be
"@cart".
Try one change first. If it doesn''t work, back it out and try the
other. If it still doesn''t work, try them both ;-)

Failing that, post back again, and I''ll have a look in my copy of the
book to see what it should be...

--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.