I bought the second edition of the ruby on rails, it seems to work fine now, did some adjustments to the code. But now I get a second error and I want to learn to understand to fix them myself. So here is the error. ArgumentError in StoreController#add_to_cart wrong number of arguments (1 for 0) #{RAILS_ROOT}/app/models/cart.rb:15:in `initialize'' #{RAILS_ROOT}/app/models/cart.rb:15:in `add_product'' #{RAILS_ROOT}/app/controllers/store_controller.rb:19:in `add_to_cart'' -e:4 Now, it says as far as I understand it, that it should need 0 arguments instead of 1. But that should be quite impossible, beacause the cart.rb has a def add_product(product) as we shall see later. I do not understand why it gives me that error. Here is the store_controller.rb: [CODE] class StoreController < ApplicationController def index @products = Product.find_products_for_sale #creates an instance variable that holds a list of all the #products in order to make it available to the code in the view #that will display the table #find_products_for_sale() IS DEFINED IN product.rb end def add_to_cart @cart = find_cart #we use find_cart method to find (or create) #a cart in this session @product = Product.find(params[:id]) #we use the params object #to get the id parameter #from the request and then #call Product model to find #the product with that id. @cart.add_product(@product) #add this product to the cart end private def find_cart unless session[:cart] #if there is no cart in sesssion add one session[:cart] = Cart.new #add a new one end session[:cart] #return existing or new cart end end [CODE] Also here is the cart.rb [CODE] class Cart include Reloadable attr_reader :items def initialize @items = [] end def add_product(product) existing_product = @items.find {|item| item.product == product} if existing_product existing_product.increment_quantity else @items << Cart.new(product) end end end [CODE] And here is the cart_item.rb [CODE] class CartItem include Reloadable attr_reader :product, :quantity def initialize(product) @product = product @quantity = 1 end def increment_quantity @quantity += 1 end def title @product.title end def price @product.price * @quantity end end [CODE] -- Posted via http://www.ruby-forum.com/.
Hi, it''s the *add_to_cart* method (not add_product(argument)) that you are calling with one argument. This method of your store controller takes *no* argument so the ruby interpreter is complaining. Cheers, Jan On 8/4/06, N/a N/a <cain_152@yahoo.com> wrote:> > I bought the second edition of the ruby on rails, it seems to work fine > now, did some adjustments to the code. But now I get a second error and > I want to learn to understand to fix them myself. So here is the error. > > ArgumentError in StoreController#add_to_cart > wrong number of arguments (1 for 0) > #{RAILS_ROOT}/app/models/cart.rb:15:in `initialize'' > #{RAILS_ROOT}/app/models/cart.rb:15:in `add_product'' > #{RAILS_ROOT}/app/controllers/store_controller.rb:19:in `add_to_cart'' > -e:4 > > Now, it says as far as I understand it, that it should need 0 arguments > instead of 1. But that should be quite impossible, beacause the cart.rb > has a def add_product(product) as we shall see later. I do not > understand why it gives me that error. Here is the store_controller.rb: > > [CODE] > class StoreController < ApplicationController > > def index > @products = Product.find_products_for_sale > #creates an instance variable that holds a list of all the > #products in order to make it available to the code in the view > #that will display the table > #find_products_for_sale() IS DEFINED IN product.rb > end > > def add_to_cart > @cart = find_cart #we use find_cart method to find (or create) > #a cart in this session > @product = Product.find(params[:id]) #we use the params object > #to get the id parameter > #from the request and then > #call Product model to find > #the product with that id. > @cart.add_product(@product) #add this product to the cart > end > private > def find_cart > unless session[:cart] #if there is no cart in sesssion add one > session[:cart] = Cart.new #add a new one > end > session[:cart] #return existing or new cart > end > end > [CODE] > > Also here is the cart.rb > [CODE] > class Cart > include Reloadable > > attr_reader :items > > def initialize > @items = [] > end > > def add_product(product) > existing_product = @items.find {|item| item.product == product} > if existing_product > existing_product.increment_quantity > else > @items << Cart.new(product) > end > end > end > [CODE] > > And here is the cart_item.rb > > [CODE] > class CartItem > include Reloadable > attr_reader :product, :quantity > > def initialize(product) > @product = product > @quantity = 1 > end > > def increment_quantity > @quantity += 1 > end > > def title > @product.title > end > > def price > @product.price * @quantity > end > > end > [CODE] > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060804/bcf7265d/attachment-0001.html
Jan Prill wrote:> Hi, > > it''s the *add_to_cart* method (not add_product(argument)) that you are > calling with one argument. > > This method of your store controller takes *no* argument so the ruby > interpreter is complaining. > > Cheers, > JanUnderstood, thanks a lot. Here is the code for the index.rhtml. The ":id => product" can be asumed to be the first parameter of the function? How can I fix this so it will work? [CODE] <h1>Your Pragmatic Catalog</h1> <% for product in @products -%> <div class="entry"> <img src="<%= product.image_url %>"/> <h3><%= h(product.title) %></h3> <%= product.description %> <h3><span class="price">Pret: <%= product.price %> RON</span></h3> <%= button_to "Add to cart", :action => :add_to_cart, :id => product %> </div> <% end %> [CODE] -- Posted via http://www.ruby-forum.com/.
Where do I call this add_to_cart method without one argument? I only call it in index.rhtml, otherwise I see not. -- Posted via http://www.ruby-forum.com/.
Hi, sorry - seems as if the problem happens on line 15 of the Cart class: @items << Cart.new(product) You''re initializing a new Cart with the argument product, but haven''t got an initialize method that takes a product as an argument. Cheers, Jan On 8/4/06, N/a N/a <cain_152@yahoo.com> wrote:> > Where do I call this add_to_cart method without one argument? I only > call it in index.rhtml, otherwise I see not. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060804/17f110d4/attachment.html
Jan Prill wrote:> Hi, > > sorry - seems as if the problem happens on line 15 of the Cart class: > @items > << Cart.new(product) > > You''re initializing a new Cart with the argument product, but haven''t > got an > initialize method that takes a product as an argument. > > Cheers, > Jannew(attributes = nil) {|self if block_given?| ...} New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table ? hence you can?t have attributes that aren?t part of the table columns. It can support attributes as far as I see her the new method. How would you write the application so it works? -- Posted via http://www.ruby-forum.com/.
Finally, I replace Cart.new(product) with CartItem.new(product). Since the Class Cart does NOT support another argument, of course it gave me an error. But CartItem DOES support a new argument ... so it works. [CODE] class CartItem include Reloadable attr_reader :product, :quantity def initialize(product) @product = product @quantity = 1 end def increment_quantity @quantity += 1 end def title @product.title end def price @product.price * @quantity end end [CODE] -- Posted via http://www.ruby-forum.com/.