Hello, I am following the guide in AWDWR for the shopping cart in
Chapter 8 Iteration C2. I have the the cart and cart_item models set up
as shown below, but am receiving the error ''undefined method
`find'' for
#<CartItem:0xb6e7d644>'' when I try to add to my cart. Here is
the
extract:
app/models/cart.rb:10:in `add_product''
app/controllers/store_controller.rb:9:in `add_to_cart''
-e:2:in `load''
-e:2
Here is my Cart model:
class Cart
attr_reader :items
def initialize
@items = []
end
def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
@items << CartItem.new(product)
end
end
end
And here is the CartItem model:
class CartItem
attr_reader :product, :quantity
def initialize(product)
@product = product
@quantity = 1
end
def increment_quantity
@quantity += 1
end
def title
@product.title
end
def price
@product.price * @quantity
end
end
Can somebody point me in the right direction as to what is throwing this
error? I tried creating a method called ''find'' in the
CartItem model
just to see if that did anything, but didn''t have much luck.
--
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?hl=en
-~----------~----~----~----~------~----~------~--~---
Never mind...found the problem. Although I am storing the session information in my database, it seems as though running ''rake db:sessions:clear'' did what it is supposed to do, but there was somehow session data in my browser yet. According to the book, one only needs to run the rake task and then refresh the page. Once I actually closed my browser and reopened it, everything worked as designed. Perhaps I have misread how storing the session data in the database instead of cookies works...Is there something that changed with Rails 2.0.2? -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
On 25 Feb 2008, at 02:19, Derek Knorr wrote:> > Never mind...found the problem. Although I am storing the session > information in my database, it seems as though running ''rake > db:sessions:clear'' did what it is supposed to do, but there was > somehow > session data in my browser yet. According to the book, one only needs > to run the rake task and then refresh the page. > > Once I actually closed my browser and reopened it, everything worked > as > designed. Perhaps I have misread how storing the session data in the > database instead of cookies works...Is there something that changed > with > Rails 2.0.2?Yes - by default in rails 2.0.2 the entire session is in the cookie. On the way you''ve found out the pitfall of putting complex objects in the session: you can get nonsensical and confusing results when the code changes but the user still has an old session with the old model. 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?hl=en -~----------~----~----~----~------~----~------~--~---