So I have a Carts model which has_many Items. When I’m on the Carts#show page I want to have a link to an action that removes an item from that cart, in other words something that will find the item and set its cart_id to nil.
What should I name this action? Which controller should it be in — CartsController or ItemsController? And lastly… how do I specify this route in my routes.rb (CODE PLEASE FOR THIS ONE)
Edit
I should be a bit more explicit: I don’t want to destroy an item when I remove it from a person’s cart. Each item is unique, and thus destroying it would destroy the listing… which is undesirable. Note that this also implies that this isn’t a HABTM relationship… items can’t belong to more than one cart.
Route:
This generates the following routes:
So you’re really explicit in describing items – carts relationship.
You use your controllers like you normally would.
I don’t know why would you want to set the
cart_idtonilinstead of removing it, but this is how you would do it: