views:

176

answers:

3

hi im using

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

from a book. this is to create a shopping cart. what I don't understand is, do I need a controller for my cart? because without it,its giving me an error

A: 

I get the feeling you're not really sure how to use Rails.

Whether or not you need a controller for you cart is up to you, however I would have one. That way you can have a "view your cart" page, as well as easily delete items.

Tyler Smith
you'd be exactly right there :/
Lily
http://railscasts.com/Watch a few of those. If you search for shopping cart (and related terms) you'll find a few episodes to watch. I don't think there are any where he actually shows how to build one, but if you look at how he has things set up you can get a good idea.
Tyler Smith
A: 

In principle you shouldn't need a controller unless you have a route for Carts inside your config/routes.rb.

khelll
A: 

Yes, the above method should definitely live inside a controller. The controller should look like this:

http://media.pragprog.com/titles/rails2/code/depot%5Ff/app/controllers/store%5Fcontroller.rb

Michiel de Mare