stephenacross-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-17 12:39 UTC
NoMethodError in StoreController#add_to_cart
I am building the Depot app from Agile. And an hoping someone can
point out the obvious that I cannot see.
** Here is the error - it occurs when I choose the "add to cart" link.
NoMethodError in StoreController#add_to_cart
You have a nil object when you didn''t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.<<
RAILS_ROOT: ./script/../config/..
#{RAILS_ROOT}/app/models/cart.rb:11:in `add_product''
#{RAILS_ROOT}/app/controllers/store_controller.rb:15:in `add_to_cart''
** controllers/store_controller.rb
class StoreController < ApplicationController
def index
@products = Product.salable_items
end
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ''display_cart'')
end
def display_cart
@cart = find_cart
@item = @cart.items
end
private
def find_cart
session[:cart] || Cart.new
end
end
** models/cart.rb
class Cart
attr_reader :items
attr_reader :total_price
def intialize
@items = []
@total_price = 0.0
end
def add_product(product)
@items << LineItem.for_product(product)
@total_price += product.price
end
end
** models/line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :product
def self.for_product(product)
item = self.new
item.quantity = 1
item.product = product
item.unit_price = product.price
item
end
end.
Thanks
-Stephen
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
stephenacross-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I am building the Depot app from Agile. And an hoping someone can > point out the obvious that I cannot see. > > class Cart > attr_reader :items > attr_reader :total_price > > def intializeYou''ve typoed here (should be initialize), and so this method is never called (and thus your Cart objects are setup correctly) Fred -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
stephenacross-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-17 16:36 UTC
Re: NoMethodError in StoreController#add_to_cart
Fred - THANKS! I have been looking at this for too many hours. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---