Forum: Ruby on Rails Wrong number of arguments?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ryan O. (Guest)
on 2008-12-30 08:22
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 %> &times; <%= 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?

Thanks!
Matt (Guest)
on 2008-12-30 08:52
(Received via mailing list)
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>
Ryan O. (Guest)
on 2008-12-30 09:18
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?
MaD (Guest)
on 2008-12-30 10:36
(Received via mailing list)
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).
Jeffrey L. Taylor (Guest)
on 2008-12-30 11:12
(Received via mailing list)
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
MaD (Guest)
on 2008-12-30 11:15
(Received via mailing list)
nice.
Ryan O. (Guest)
on 2008-12-30 17:54
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:in `new'
app/models/cart.rb:13:in `add_product'
app/controllers/store_controller.rb:10:in `add_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:in
`perform_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:in
`perform_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:in
`measure'
/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:in
`perform_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:in
`process_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:in
`process'
/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:in
`dispatch'
/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:in
`synchronize'
/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:in
`process_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:in
`process_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:in
`initialize'
/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:in `run'
/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:in `new'
/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:in
`run'
/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:in
`run'
/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:in
`run'
/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:in
`load'
/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:in
`load'
/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:in `require'
/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:in
`new_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:in
`gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3
Freddy A. (Guest)
on 2008-12-30 18:02
(Received via mailing list)
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
Ryan O. (Guest)
on 2008-12-30 18:22
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$
Ryan O. (Guest)
on 2008-12-30 19:21
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?
Freddy A. (Guest)
on 2008-12-30 21:05
(Received via mailing list)
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
This topic is locked and can not be replied to.