That means that somewhere, you are calling a method that doesn’t expect any arguments and you are passing an argument to it. The stack trace give you a clue:
One of the methods listed (#add_to_cart, #initialize, #new, or #add_product) is being called with an argument where it wasn’t expecting one.
StoreController#add_to_cart
– doesn’t expect any methods, but this is called by the Rails framework as an action, and Rails doesn’t pass any arguments to the action methods
Cart#add_product
– expects 1 argument, you’re calling it with 1 argument in StoreController#add_to_cart
CartItem#new
– calls CartItem#initialize with however many arguments it was passed
– is called with 1 argument in Cart#add_product
CartItem#initialze
– expects 0 arguments, you passed 1 argument in via CartItem#new
StoreController#add_to_cart
-- doesn't expect any methods, but this is called by the Rails framework
as
an action, and Rails doesn't pass any arguments to the action methods
Cart#add_product
-- expects 1 argument, you're calling it with 1 argument in
StoreController#add_to_cart
CartItem#new
-- calls CartItem#initialize with however many arguments it was passed
-- is called with 1 argument in Cart#add_product
CartItem#initialze
-- expects 0 arguments, you passed 1 argument in via CartItem#new
I'm not really sure what do do with that -- what should I change? Are
you saying only my CartItem#initialze is wrong?
– expects 0 arguments, you passed 1 argument in via CartItem#new
I’m not really sure what do do with that – what should I change? Are
you saying only my CartItem#initialze is wrong?
Probably.
You could always add a parameter to your CartItem#initialize method. I’m not sure that you’re heading in the direction you want to be heading, since it appears that CartItem is not derived from ActiveRecord::Base – it’s not tied to a database.
You might want to go back and study Rail’s ideas regarding models, how they tie to database tables, and how you can initialize them.
You could always add a parameter to your CartItem#initialize method.
I'm
not sure that you're heading in the direction you want to be heading,
since
it appears that CartItem is not derived from ActiveRecord::Base -- it's
not
tied to a database.
You might want to go back and study Rail's ideas regarding models, how
they
tie to database tables, and how you can initialize them.
I following along from the book DHH wrote verbatim, but it's the second
edition. It was my understanding that having a database table for the
cart was unnecessary because it's session data and would be avaliable
based on that user?
Looks like you have two open posts for the same issue... Here is the
answer from the other post...
ahhhh Sorry I see now I was thinking that the CartItem class was an
activerecord class ... The issue is this:
Here you call a new object of CartItem with a product passed
@items << CartItem.new(product)
But here in the initialize you do not have a argument for
initialize...
class CartItem
attr_reader :product, :quantity
def initialize
@product = product
@quantity = 1
end
Change it to this
class CartItem
attr_reader :product, :quantity
def initialize( product )
@product = product
@quantity = 1
end
I would NEVER store the cartitem(add product to cart) in a session
object for a ecommerce application. Yes there are things that could/
should be stored in the session but not something like adding a
product to your cart... Unless you have a session replication system
for your back-end which then would store the session in either the
database or memcache anywho...
ahhhh Sorry I see now I was thinking that the CartItem class was an
activerecord class ... The issue is this:
Here you call a new object of CartItem with a product passed
@items << CartItem.new(product)
But here in the initialize you do not have a argument for
initialize...
class CartItem
attr_reader :product, :quantity
def initialize
@product = product
@quantity = 1
end
Change it to this
class CartItem
attr_reader :product, :quantity
def initialize( product )
@product = product
@quantity = 1
end
Interesting, tried that and it's spitting a "no method for cart_item
back at me:
NameError in Store#add_to_cart
Showing app/views/store/add_to_cart.rhtml where line #4 raised:
undefined local variable or method `cart_item' for
#<#<Class:0x2602924>:0x26028e8>
Extracted source (around line #4):
1: <h1>The shopping cart</h1>
2: <ul>
3: <% for item in @cart.items %>
4: <li><%= cart_item.quantity %> × <%= h(item.title) %></li>
5: <% end %>
6: </ul>
RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace
Where does the cart_item come from? is that in the Store controller?
post the store controller... The view has the @cart object but also
needs the cart_item.. Is that from the session?