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? Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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? > > Thanks! > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Ororie <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>How can I do that? -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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). --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Quoting Ryan Ororie <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: [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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
nice. On 30 Dez., 10:12, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> Quoting Ryan Ororie <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: > [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--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- 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 -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Freddy Andersen 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 > endThis 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$ -- 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 -~----------~----~----~----~------~----~------~--~---
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? -- 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 -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---