undefined method `items' for nil:NilClass

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 %>   <% @cameratypes.each do |camera| -%>   <% @firstoftype = find_first_image_in_type(camera.camtype)%>   <%= link_to(image_tag(@firstoftype.image_url), showmakes_url(:id => camera.camtype)) %>   <div>     <h3><%=h @firstoftype.image_url + ' ' + camera.camtype%></h3>   </div>   <% end %> <% end %>

******************_cart partial*********************

<div class="cart_title">Your Cart</div> <table>   <%= render(:partial => "cart_item", :collection => cart.items) %>

  <tr class="total-line">     <td colspan="2">Total</td>     <td class="total-cell"><%= number_to_currency(cart.total_price) %></td>   </tr> </table>

<%= button_to 'Empty cart', :action => 'empty_cart' %>

*******************_cart_item partial**************

<tr>   <td><%= cart_item.quantity %> &times; <%=h cart_item.make %> <%=h cart_item.model %></td>   <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>   <% @firstofmake = find_first_image_in_make(camera.make)%>   <%= link_to(image_tag(@firstofmake.image_url), showmodels_url(:typeid => camera.camtype, :makeid => camera.make)) %>   <h3><%=h camera.make%></h3> </div> <% end %>

********************showmodels view****************

<% @cameramodels.each do |camera| -%> <div class="entry">   <fieldset>     <legend>     <h3><%= image_tag(camera.image_url) %></h3>     <h3><%=h camera.model%></h3>     <h3><%=h camera.description %></h3>     <div class="price-line">       <span class="price"><%= number_to_currency(camera.price) %></span>       <% form_remote_tag :url => {:action => 'add_to_cart', :id => camera } do %>         <%= submit_tag "Add to Cart" %>       <% end %>     </div>     </legend>   </fieldset> </div> <% end %>

*********************store controller**************

class StoreController < ApplicationController

  helper_method :find_first_image_in_type, :find_first_image_in_make

  def index     @cameras = Camera.find_cameras_for_sale     @cameratypes = Camera.find_cameras_by_camtype   end

  def find_first_image_in_type(camtype)     Camera.find_first_type_image(camtype)   end

  def find_first_image_in_make(cammake)     Camera.find_first_make_image(cammake)   end

  def showmakes     @camtype = params[:id]     @camerasbytype = Camera.find_cameras_of_type(@camtype)   end

  def showmodels     @camtype = params[:typeid]     @cammake = params[:makeid]     @cameramodels = Camera.find_cameras_by_make_and_type(@camtype, @cammake)     @cart = find_cart   end

  def add_to_cart     camera = Camera.find(params[:id])     @cart = find_cart     @cart.add_camera(camera)     respond_to do |format|       format.js     end     redirect_to_index   rescue     logger.error("Attempt to access invalid camera #{params[:id]}")     redirect_to_index("Invalid camera")   end

  def empty_cart     session[:cart] = nil     redirect_to_index("Your cart is currenty empty")   end

  private   def find_cart     session[:cart] ||= Cart.new   end

  def redirect_to_index(msg = nil)     flash[:notice] = msg if msg     redirect_to :action =>'index'   end end

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 :wink:

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

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