undefined method `items'' for nil:NilClass

Evanoshki Brataslavainskinski
Wed, 20 Oct 2010 08:17:03 -0000

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: Your Cart
2:
3:   <%= render(:partial => "cart_item", :collection =>
cart.items) %>
4:
5:
6: Total

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

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

Your Pragmatic Catalog

<% 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)) %>

<%=h @firstoftype.image_url + '' '' +
camera.camtype%>

  <% end %>
<% end %>

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

Your Cart

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


Total
<%=
number_to_currency(cart.total_price)
%>



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

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


<%= cart_item.quantity %> &times; <%=h cart_item.make
%> <%=h
cart_item.model %>
<%=
number_to_currency(cart_item.price) %>


*******************add_to_cart.js.rjs***************

page.replace_html("cart", :partial => "cart", :object
=> @cart)

*******************showmakes view*******************

Your Pragmatic Catalog

<% @camerasbytype.each do |camera| -%>

  <% @firstofmake = find_first_image_in_make(camera.make)%>
  <%= link_to(image_tag(@firstofmake.image_url), showmodels_url(:typeid
=> camera.camtype, :makeid => camera.make)) %>
<%=h camera.make%>

<% end %>

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

<% @cameramodels.each do |camera| -%>



<%= image_tag(camera.image_url) %>
<%=h camera.model%>
<%=h camera.description %>

<%=
number_to_currency(camera.price) %>
      <% form_remote_tag :url => {:action =>
''add_to_cart'', :id =>
camera } do %>
        <%= submit_tag "Add to Cart" %>
      <% end %>




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

***********************************************

I don''t want to shower you with unnecessary code.. It is difficult for
me to trace the error to a specific model, view or controller.. So
please if you want can''t determine the problem from this.. Let me know
and I will give you more information

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