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