Reputation: 51
Given a typical nested resource of a photo with comments, the route to create a comment would look something like:
POST /photos/{photo_id}/comments
Now, for deleting the comment, would you still use a "nested" route? ex:
DELETE /photos/{photo_id}/comments/{comment_id}
or
DELETE /comments/{comment_id}
The pro of the nested route is that it mirrors the creation URL and doesn't require any additional entries in routes.rb. The pro of using a top-level URL is that you technically don't need the photo_id to get the comment to delete.
Thoughts?
Upvotes: 1
Views: 631
Reputation: 6898
How you model the comments
resource depends heavily how you see the resource comments
.
In case a comment could exist without a photo and could be associated with 0 to N resources like photo
then you should model your comments like this
GET /comments/{comment_id}
DELETE /comments/{comment_id}
PUT /comments/
POST /comments/{comment_id}/associations/photo/{photo_id}
In case a comment is always associated with a resource and cannot exist without being associated with a resource then you should stick with
POST /photos/{photo_id}/comments
DELETE /photos/{photo_id}/comments/{comment_id}
I guess the confusion how to model the comment is driven by the database model where every comment gets a unique id that is unique among all comments and not just unique in a photo_id
and comment_id
combined key. I suggest not to let the database model leak to the resource model and go for a model that fits your conceptional understanding of the resource.
Upvotes: 4
Reputation:
Can you GET
this?
GET /comments/{comment_id}
I guess not. But if you can't GET
a resource, you can't DELETE
it, too.
So only your second option is RESTful.
Upvotes: 2
Reputation: 8604
Personally, I'm just using nested route for delete, means i used:
DELETE /photos/{photo_id}/comments/{comment_id}
to delete comments of a photo. If i use:
DELETE /comments/{comment_id}
so i have to create one more route for this? I don't find any reasons to create a separate route for delete, I think it's not necessary. The nested resources have create url and path for us, and they are follow convention, why don't we use them? I just want to keep it simple and will not do extra work for things already have.
Upvotes: 1