# REST help

**URL:** <https://www.ruby-forum.com/t/rest-help/117619>\
**Category:** Rails\
**Created:** [October 18, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/rest-help/117619 "2007-10-18T18:31:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [October 18, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/rest-help/117619/1 "2007-10-18T18:31:46Z")

</div>

Hi all,

I am trying to build a RESTful shopping cart and I have hit a small  
wall, but before I get to that let me explain what I have done thus far.

I have two RESTful controllers, CartController and CartItemsController  
with a nested routes setup like so

map.resources :carts do |carts|  
carts.resources :cart\_items, :collection =\> {:empty =\> :delete}  
end

(I added a custom route to the for emptying the cart as it seemed to me  
that when I clear the cart I am operating on the items which is why I  
added that action to the CartItemsController. If this is not RESTfully  
“correct” I would love to hear about it!)

Using the nested routes I am using the CartItemsController to perform  
the CRUD operations on CartItems and assigning them to the proper cart  
(e.g. adding an item would link to POST /carts/1/cart\_items). When a  
user attempts to add an item to the cart that is already there, from  
what I understand of REST, this should be an update (i.e. PUT  
/cart/1/cart\_items/23) with the new attributes for the item. All I  
should have to do is change the submission form where the user is doing  
the update from a POST to a PUT depending on whether the item they are  
about to add is in the cart already.

BUT, what if I am using AJAX to update the cart? When the form is  
initally generated it will use a create action. When the user adds the  
item to cart, the page does not refresh and the form retains it create  
action. If the user uses the form again the item will become a new line  
item in the cart. Do you see where I am going with this?

Possible solutions I have kicked around is doing a check in the  
CartItemsController create action to see if the item is already in the  
cart and the update it, but that seems just plain wrong. Using RJS  
updating to update the form with a new URL and additional field with the  
CartItem ID or creating a custom action to handle all of this (seems  
wrong too!)

I am really starting to enjoy using RESTful development, but I think I  
just know enough to be dangerous so if anyone could give me some advice  
on the more ‘correct’ way to solve this problem that would be fantastic.  
Also, if anyone knows of really good resource about resources (like how  
to identify) please pass it along.

Thanks

Peer
