路线:
resources :carts do
resources :items
end
这会生成以下路线:
cart_items GET /carts/:cart_id/items(.:format) items#index
POST /carts/:cart_id/items(.:format) items#create
new_cart_item GET /carts/:cart_id/items/new(.:format) items#new
edit_cart_item GET /carts/:cart_id/items/:id/edit(.:format) items#edit
cart_item GET /carts/:cart_id/items/:id(.:format) items#show
PUT /carts/:cart_id/items/:id(.:format) items#update
DELETE /carts/:cart_id/items/:id(.:format) items#destroy
carts GET /carts(.:format) carts#index
POST /carts(.:format) carts#create
new_cart GET /carts/new(.:format) carts#new
edit_cart GET /carts/:id/edit(.:format) carts#edit
cart GET /carts/:id(.:format) carts#show
PUT /carts/:id(.:format) carts#update
DELETE /carts/:id(.:format) carts#destroy
所以你在描述物品 - 购物车关系方面真的很明确。
您可以像往常一样使用控制器。
我不知道您为什么要将其设置cart_id
为nil
而不是删除它,但这就是您的做法:
# in items_controller.rb
def destroy
@item = Item.find(params[:id])
@item.update_attributes(:cart_id => params[:cart_id])
respond_to do |format|
format.html { redirect_to items_url }
format.json { head :no_content }
end
end