# undefined method \`items' for nil:NilClass

**URL:** <https://discuss.rubyonrails.org/t/undefined-method-items-for-nil-nilclass/55216>\
**Category:** rubyonrails-talk\
**Created:** [October 20, 2010, 8:17am UTC](https://discuss.rubyonrails.org/t/undefined-method-items-for-nil-nilclass/55216 "2010-10-20T08:17:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 20, 2010, 8:17am UTC](https://discuss.rubyonrails.org/t/undefined-method-items-for-nil-nilclass/55216/1 "2010-10-20T08:17:03Z")

</div>

Hi everybody..

I would appreciate it very much if someone can guide me in the right direction in solving this problem I have been experiencing.

The problem occurs when I want to add to cart.

I am following the guide in "Agile web development with rails". It was working before, but then the application needed some AJAX and also two of the views needed to be converted to partials.

Now I can't get it to work anymore..

It is bound to be some small problem somewhere that I have been blind to..

It occurs when I try to load the store index page..

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Error Message\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

NoMethodError in Store#showmakes

Showing app/views/store/\_cart.html.erb where line #3 raised:

undefined method `items' for nil:NilClass

Extracted source (around line #3):

1: \<div class="cart\_title"\>Your Cart\</div\> 2: \<table\> 3: \<%= render(:partial =\> "cart\_item", :collection =\> cart.items) %\> 4: 5: \<tr class="total-line"\> 6: \<td colspan="2"\>Total\</td\>

Trace of template inclusion: app/views/layouts/cameras.html.erb

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*store index(Working)\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<h1\>Your Pragmatic Catalog\</h1\>

\<% if @cameras.count \> 1 then %\> &nbsp;&nbsp;\<% @cameratypes.each do |camera| -%\> &nbsp;&nbsp;\<% @firstoftype = find\_first\_image\_in\_type(camera.camtype)%\> &nbsp;&nbsp;\<%= link\_to(image\_tag(@firstoftype.image\_url), showmakes\_url(:id =\> camera.camtype)) %\> &nbsp;&nbsp;\<div\> &nbsp;&nbsp;&nbsp;&nbsp;\<h3\>\<%=h @firstoftype.image\_url + ' ' + camera.camtype%\>\</h3\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<% end %\> \<% end %\>

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\_cart partial\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<div class="cart\_title"\>Your Cart\</div\> \<table\> &nbsp;&nbsp;\<%= render(:partial =\> "cart\_item", :collection =\> cart.items) %\>

&nbsp;&nbsp;\<tr class="total-line"\> &nbsp;&nbsp;&nbsp;&nbsp;\<td colspan="2"\>Total\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td class="total-cell"\>\<%= number\_to\_currency(cart.total\_price) %\>\</td\> &nbsp;&nbsp;\</tr\> \</table\>

\<%= button\_to 'Empty cart', :action =\> 'empty\_cart' %\>

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\_cart\_item partial\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<tr\> &nbsp;&nbsp;\<td\>\<%= cart\_item.quantity %\> &times; \<%=h cart\_item.make %\> \<%=h cart\_item.model %\>\</td\> &nbsp;&nbsp;\<td class="item-price"\>\<%= number\_to\_currency(cart\_item.price) %\>\</td\> \</tr\>

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*add\_to\_cart.js.rjs\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

page.replace\_html("cart", :partial =\> "cart", :object =\> @cart)

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*showmakes view\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<h1\>Your Pragmatic Catalog\</h1\>

\<% @camerasbytype.each do |camera| -%\> \<div\> &nbsp;&nbsp;\<% @firstofmake = find\_first\_image\_in\_make(camera.make)%\> &nbsp;&nbsp;\<%= link\_to(image\_tag(@firstofmake.image\_url), showmodels\_url(:typeid =\> camera.camtype, :makeid =\> camera.make)) %\> &nbsp;&nbsp;\<h3\>\<%=h camera.make%\>\</h3\> \</div\> \<% end %\>

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*showmodels view\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<% @cameramodels.each do |camera| -%\> \<div class="entry"\> &nbsp;&nbsp;\<fieldset\> &nbsp;&nbsp;&nbsp;&nbsp;\<legend\> &nbsp;&nbsp;&nbsp;&nbsp;\<h3\>\<%= image\_tag(camera.image\_url) %\>\</h3\> &nbsp;&nbsp;&nbsp;&nbsp;\<h3\>\<%=h camera.model%\>\</h3\> &nbsp;&nbsp;&nbsp;&nbsp;\<h3\>\<%=h camera.description %\>\</h3\> &nbsp;&nbsp;&nbsp;&nbsp;\<div class="price-line"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<span class="price"\>\<%= number\_to\_currency(camera.price) %\>\</span\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% form\_remote\_tag :url =\> {:action =\> 'add\_to\_cart', :id =\> camera } do %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= submit\_tag "Add to Cart" %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\</div\> &nbsp;&nbsp;&nbsp;&nbsp;\</legend\> &nbsp;&nbsp;\</fieldset\> \</div\> \<% end %\>

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*store controller\*\*\*\*\*\*\*\*\*\*\*\*\*\*

class StoreController \< ApplicationController

&nbsp;&nbsp;helper\_method :find\_first\_image\_in\_type, :find\_first\_image\_in\_make

&nbsp;&nbsp;def index &nbsp;&nbsp;&nbsp;&nbsp;@cameras = Camera.find\_cameras\_for\_sale &nbsp;&nbsp;&nbsp;&nbsp;@cameratypes = Camera.find\_cameras\_by\_camtype &nbsp;&nbsp;end

&nbsp;&nbsp;def find\_first\_image\_in\_type(camtype) &nbsp;&nbsp;&nbsp;&nbsp;Camera.find\_first\_type\_image(camtype) &nbsp;&nbsp;end

&nbsp;&nbsp;def find\_first\_image\_in\_make(cammake) &nbsp;&nbsp;&nbsp;&nbsp;Camera.find\_first\_make\_image(cammake) &nbsp;&nbsp;end

&nbsp;&nbsp;def showmakes &nbsp;&nbsp;&nbsp;&nbsp;@camtype = params[:id] &nbsp;&nbsp;&nbsp;&nbsp;@camerasbytype = Camera.find\_cameras\_of\_type(@camtype) &nbsp;&nbsp;end

&nbsp;&nbsp;def showmodels &nbsp;&nbsp;&nbsp;&nbsp;@camtype = params[:typeid] &nbsp;&nbsp;&nbsp;&nbsp;@cammake = params[:makeid] &nbsp;&nbsp;&nbsp;&nbsp;@cameramodels = Camera.find\_cameras\_by\_make\_and\_type(@camtype, @cammake) &nbsp;&nbsp;&nbsp;&nbsp;@cart = find\_cart &nbsp;&nbsp;end

&nbsp;&nbsp;def add\_to\_cart &nbsp;&nbsp;&nbsp;&nbsp;camera = Camera.find(params[:id]) &nbsp;&nbsp;&nbsp;&nbsp;@cart = find\_cart &nbsp;&nbsp;&nbsp;&nbsp;@cart.add\_camera(camera) &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.js &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to\_index &nbsp;&nbsp;rescue &nbsp;&nbsp;&nbsp;&nbsp;logger.error("Attempt to access invalid camera #{params[:id]}") &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to\_index("Invalid camera") &nbsp;&nbsp;end

&nbsp;&nbsp;def empty\_cart &nbsp;&nbsp;&nbsp;&nbsp;session[:cart] = nil &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to\_index("Your cart is currenty empty") &nbsp;&nbsp;end

&nbsp;&nbsp;private &nbsp;&nbsp;def find\_cart &nbsp;&nbsp;&nbsp;&nbsp;session[:cart] ||= Cart.new &nbsp;&nbsp;end

&nbsp;&nbsp;def redirect\_to\_index(msg = nil) &nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = msg if msg &nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\>'index' &nbsp;&nbsp;end end

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [October 20, 2010, 8:38am UTC](https://discuss.rubyonrails.org/t/undefined-method-items-for-nil-nilclass/55216/2 "2010-10-20T08:38:28Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 20, 2010, 9:04am UTC](https://discuss.rubyonrails.org/t/undefined-method-items-for-nil-nilclass/55216/3 "2010-10-20T09:04:35Z")

</div>

Very good suggestion Michael.. Thanks for your help.. I have deviated a little from the book, since there are more functions that I need to incorporate.. I tried putting it in the index def in the store controller, but no luck.. Tried putting the @cart.find\_cart in the controller but no luck.. I believe the add\_to\_cart.js\_rjs renders the cart partial. Don't no why the index would render or look for it..

The add\_to\_cart function in the store controller is called by a button on the page 'showmodels'. In the handbook the @cart.find\_item is located in the add\_to\_cart function
