@cart.add_product(product)

Any one can explain me???

in agile web development book in chapter Cart Creation

depot_f/app/models/cart.rb

class Cart
attr_reader :items
def initialize
@items = []
end

def add_product(product)
@items << product
end
end

depot_f/app/views/store/index.html.erb

<%= button_to ‘Add to Cart’, :action => ‘add_to_cart’, :id => product %>

depot_f/app/controllers/store_controller.rb

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

when use will click on Add_to_cart button in index page then width
particular product id add_to_cart action take placed in
store_controller.rb file.After finding the array of row from from
table product and assign to product variable. then find_cart method is
called in same controller which is private and create OR initialize
session. then add_product method is called width product variable then
it will goes to cart.rb model and then execute def

add_product(product)
@items << product
end

please explain me why add_product(product) method in store_controller
will go in Cart class in model cart.rb why not in other class.

thanks

On 26 August 2010 07:04, pravin mishra [email protected]
wrote:

end
depot_f/app/controllers/store_controller.rb
table product and assign to product variable. then find_cart method is
called in same controller which is private and create OR initialize
session. then add_product method is called width product variable then
it will goes to cart.rb model and then execute def

add_product(product)
@items << product
end

please explain me why add_product(product) method in store_controller
will go in Cart class in model cart.rb why not in other class.

add_product is called by the line
@cart.add_product(product)
@cart is a Cart object so it calls the add_product method of the Cart
class. Perhaps you should do a bit of reading up on basic Ruby. The
Ruby Pickaxe book is very good.

Colin