REST path for "new from copy"

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

I am wondering what the correct calm route will be for this.

My initial thinking is that this may be a parameter to the new action. That is, to borrow examples from Rails Guides , and not just:

GET: / photos / new

Also allow:

GET: / photos / new /: id

... where :id is the identifier of the entry used as a template. The answer will be a new / editable form, the same as with the plain old new , but the values โ€‹โ€‹will be pre-populated with data from an existing record. A parameter (or its absence) can be easily processed using the new controller method.

An alternative, apparently, is to create a new controller method, for example, copy , which will also accept the identifier of the existing record and response with a new form, as indicated above. For me, this seems a bit โ€œwrongโ€, since the record is not actually copied until the user saves the new record (after it is likely to change it a little).

TIA ...

UPDATE: my question is not "how to do it on rails?", It is "RESTful?"

+7
source share
4 answers

My question is not "how to do it on rails?", "Is it RESTful?"

No, it is not. In this case, no GET /photos/new . Rails seems to be hopelessly mired in the past, where it was considered a high GET program in a URI to return an HTML form, which then returns the POST x-www-form-urlencoded data back to the same URI. The opacity of this POST forces them to invent new verbs-like-URIs such as /photos/new when you could use PUT instead or at least POST with the same media type.

The easiest way to make a copy of the HTTP RESTfully resource:

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

If you implement this for browsers, you can easily follow these steps through Ajax, using an HTML page sitting perhaps in /photos/manager.html/ to control user interaction.

+3
source

You can try to use the enclosed resources. I'm not quite sure about the structure of your application, but in general, using attached photos will look something like this:

routes.rb

 resources :photos do resources :photos end 

photos_controller.rb

 before_filter :find_parent_photo, :only => [:new, :create] def create @photo = Photo.new params[:photo] if @parent_photo.present? # fill some @photo fields from @parent_photo end @photo.save respond_with @photo end def find_parent_photo @parent_photo = Photo.find(params[:photo_id]) if params[:photo_id].present? end 

new.html.haml

 = form_for [@parent_photo, @photo] do |f| -# your form code 

earlier, when you wanted to add a link to create a photo, you wrote something like this

 = link_to "new photo", [:new, :photo] 

now if you want to add a link to create a photo based on foto @photo1

 = link_to "new photo based on other one", [:new, @photo1, :photo] 
+1
source

You should have match a route like this:

 match 'photos/new/:photo_id' => 'photos#new 

or you can just pass the :photo_id parameter to the url and process it in the controller:

 '/photos/new?photo_id=17' 

Example using the helper method: new_photo_path(:photo_id => 17)

Edit: I don't know if this is REST compliant

+1
source

It may be on top, but you can do something like this:

 class PhotoCopiesController < ApplicationController def new @photo = Photo.find(params[:photo_id]).dup end def create end end 

and

 resources :photo_copies, :only => [:new, :create] 

and

 = link_to 'Copy', photo_copy_path(:photo_id => @photo.id) 
+1
source

All Articles