I'm going through a tutorial here and running into an error I'm not sure
how to fix..this is my first foire into sessions.
Trying to create a shopping cart for a store app. I'm getting this
error:
wrong number of arguments (1 for 0)
RAILS_ROOT: script/../config/..
Here is my cart_item.rb model:
class CartItem
attr_reader :product, :quantity
def initialize
@product = product
@quantity = 1
end
def increment_quantity
@quantity += 1
end
def title
@product.title
end
def price
@product.price * @quantity
end
end
And my cart.rb 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 my add_to_cart.rhtml view:
<h1>The shopping cart</h1>
<ul>
<% for item in @cart.items %>
<li><%= cart_item.quantity %> × <%= h(item.title) %></li>
<% end %>
</ul>
When I first tried clicking the add to cart function for a product I got
an error that method 'product' was undefined but then I did rake
db:sessions:clear and now I am getting this wrong number of arguments
error - any ideas?
You're going to have to isolate what line the error originates from on
your application by going through the Rails trace information,
otherwise we're all going to be here forever trying to figure out what
the problem is.
You're going to have to isolate what line the error originates from on
your application by going through the Rails trace information,
otherwise we're all going to be here forever trying to figure out what
the problem is.
On Dec 30, 1:22�am, Ryan Ororie <rails-mailing-l...@andreas-s.net>
when you get the error mentioned above, there should be the rails
trace right on the same page (the part where rails tells you in which
line of your code and on which method the error occurred).
Quoting Ryan Ororie <rails-mailing-list@andreas-s.net>:
[snip]
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
Your code is calling the ActiveRecord find() which takes an argument or more.
You are intending the Array method. It has an alias, detect. Change the line
above to:
when you get the error mentioned above, there should be the rails
trace right on the same page (the part where rails tells you in which
line of your code and on which method the error occurred).
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
Try changing this to
def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
item = CartItem.create(product)
@items << item
end
end
This changed my error to:
undefined method `create' for CartItem:Class
Might be worth nothing that when I try to clear the session this is what
I get:
bio4054059:depot rmorourk$ rake db:sessions:clear
(in /Users/rmorourk/Sites/depot)
/Users/rmorourk/Sites/depot/config/boot.rb:26:Warning:
Gem::SourceIndex#search support for String patterns is deprecated
bio4054059:depot rmorourk$