Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST path for "new from copy"

For certain models, I wish to provide functionality that allows a user to create a new record with default attributes based on copy of an existing record.

I'm wondering what would be the correct restful route for this.

My initial thinking is that it could be a parameter to the new action. I.e. to borrow from the the Rails Guides examples, instead of just:

GET : /photos/new

Also allow:

GET : /photos/new/:id

...where :id is the id of the record to use as a template. The response would be a new/edit form, same as with a plain old new but the values would be pre-filled with data from the existing record. The parameter (or absense of it) could be easily handled by the new controller method.

The alternative seems to be to create a new controller method, for example copy which would also accept an id of an existing record and response with the new form as above. This seems a little 'incorrect' to me, as the record is not actually being copied until the user saves the new record (after probably editig it somewhat).

TIA...

UPDATE: my question is not "how do I do this in rails?", it's "is it RESTful?"

like image 306
Robert Brown Avatar asked Aug 31 '11 01:08

Robert Brown


1 Answers

my question is not "how do I do this in rails?", it's "is it RESTful?"

No, it isn't. For that matter, neither is GET /photos/new. Rails seems to be hopelessly mired in the past, where it was considered haute programme for a GET on a URI to return an HTML form which would then POST x-www-form-urlencoded data back to that same URI. The opacity of that POST forces them to invent new verbs-as-URI's like /photos/new, when you could be using PUT instead, or at least POST with the same media type.

The simplest way to make a copy of an HTTP resource RESTfully is:

GET /photos/{id}/ -> [representation of a photo resource]
...make modifications to that representation as desired...
POST /photos/ <- [modified representation]

If you're implementing this for browsers, you should be able to perform those actions via Ajax quite easily, using an HTML page sitting perhaps at /photos/manager.html/ to drive the interaction with the user.

like image 59
fumanchu Avatar answered Nov 17 '22 12:11

fumanchu