Wrong number of arguments?


#1

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:

The shopping cart

    <% for item in @cart.items %>
  • <%= cart_item.quantity %> × <%= h(item.title) %>
  • <% end %>

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?

Thanks!


#2

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 O. removed_email_address@domain.invalid


#3

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).


#4

Matt wrote:

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 O. removed_email_address@domain.invalid

How can I do that?


#5

Quoting Ryan O. removed_email_address@domain.invalid:
[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:

current_item = @items.detect {|item| item.product == product}

Someone on this list answer my similar question a year or two ago. I
pass the
“each one, teach one” obligation on to you.

Grin,
Jeffrey


#6

MaD wrote:

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).

Here is the full trace:

app/models/cart.rb:13:in initialize' app/models/cart.rb:13:innew’
app/models/cart.rb:13:in add_product' app/controllers/store_controller.rb:10:inadd_to_cart’
/Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in
send' /Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:inperform_action_without_filters’
/Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in
call_filter' /Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:inperform_action_without_benchmark’
/Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in
perform_action_without_rescue' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in
perform_action_without_rescue' /Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:inperform_action’
/Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in
send' /Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:inprocess_without_filters’
/Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in
process_without_session_management_support' /Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:inprocess’
/Users/rmorourk/.gem/ruby/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in
process' /Users/rmorourk/.gem/ruby/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:indispatch’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:76:in
process' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:insynchronize’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in
process' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:inprocess_client’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in each' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:inprocess_client’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in run' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:ininitialize’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in new' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:inrun’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in
initialize' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:innew’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in run' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:inrun’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in
each' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:inrun’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in run' /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:inrun’
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281
/Users/rmorourk/.gem/ruby/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in
load' /Users/rmorourk/.gem/ruby/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:inload’
/Users/rmorourk/.gem/ruby/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in
new_constants_in' /Users/rmorourk/.gem/ruby/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:inload’
/Users/rmorourk/.gem/ruby/1.8/gems/rails-1.2.3/lib/commands/servers/mongrel.rb:60
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in
gem_original_require' /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:inrequire’
/Users/rmorourk/.gem/ruby/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in
require' /Users/rmorourk/.gem/ruby/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:innew_constants_in’
/Users/rmorourk/.gem/ruby/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in
require' /Users/rmorourk/.gem/ruby/1.8/gems/rails-1.2.3/lib/commands/server.rb:39 /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:ingem_original_require’
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require’
script/server:3


#7

nice.


#8

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


#9

Freddy A. wrote:

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$


#10

It might also be helpful to see my store_controller.rb file:

class StoreController < ApplicationController

def index
@products = Product.find_products_for_sale
end

def add_to_cart
@cart = find_cart
product = Product.find(params[:id])
@cart.add_product(product)
end

private
def find_cart
session[:cart] ||=Cart.new
end
end

If I am reading the trace right (which I don’t know that I am) I think
the problem is on the @cart.add_product(product) line?


#11

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