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 section I am working on involves the creation of a shopping 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:
2:
Total |
Total | <%= number_to_currency(cart.total_price) %> |
<%= button_to ‘Empty cart’, :action => ‘empty_cart’ %>
*****_cart_item partial
****add_to_cart.js.rjs
page.replace_html(“cart”, :partial => “cart”, :object => @cart)
showmakes view
Your Pragmatic Catalog
<% @camerasbytype.each do |camera| -%>
<%=h camera.make%>
****showmodels view
<% @cameramodels.each do |camera| -%>
<%= image_tag(camera.image_url) %>
<%=h camera.model%>
<%=h camera.description %>
*******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 can’t determine the problem from the code provided… Let
me know and I will give you more information