简体   繁体   中英

shopping cart ruby on rails

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

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.

原则上,除非你有一个路由,你不应该需要一个控制器Carts你的config / routes.rb文件内。

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

http://media.pragprog.com/titles/rails2/code/depot_f/app/controllers/store_controller.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM