On 26 August 2010 07:04, pravin mishra
<diatm.pravin.it.07.27-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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.
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
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.