5

I'd like to delete multiple objects of the same type using a RESTful controller.

The most simple thing I can think of is to have the destroy action expect a comma-separated list of ids of objects to destroy.

Is there a more elegant way to do this?

3 Answers 3

3

I think it would be more elegant to take an array of ids:

http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters

Sign up to request clarification or add additional context in comments.

Comments

2

You could use nested forms for it..

See http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

I think thats the most elegant version...

<% form_for @person do |person_form| %>

  <%= person_form.label :name %>
  <%= person_form.text_field :name %>

   <% person_form.fields_for :children do |child_form| %>

     <%= child_form.label :name %>
     <%= child_form.text_field :name %>

     <% unless child_form.object.new_record? %>
     <%= child_form.check_box '_delete' %>
     <%= child_form.label '_delete', 'Remove' %>
   <% end %>
  <% end %>

  <%= submit_tag %>
<% end %>

Comments

-1

Here's how the RESTful request might look.

POST /posts/delete_multiple HTTP/1.1
Host: www.example.com

post_ids[]=33&post_ids[]=47&post_ids[]=88

Note that while GET, PUT, and DELETE have very specific meanings in the context of REST, POST is more vague and essentially means to take some action. The action to take is given in the URL and additional data specific to the action are passed in the entity (body) of the request. Only use POST in this manner when GET, PUT, and DELETE do not have the intended meaning.

POST is commonly interpreted as "create", but this is not really correct. We commonly use POST for creating new resources when the client doesn't know what the URL of the newly created resource should be. But when the client does get to determine the URL of the newly created resource, the correct verb would be PUT.

3 Comments

The example given is an RPC style architecture where the intended operation is defined in the URI rather than by the HTTP method. Also, POST is clearly defined in section 9.5 of the HTTP standard. Although the standard does indicate that POST can be used to submit data to data handling process that does not make that use of the POST method RESTful. A RESTful approach would be to define a new resource that represents a collection of other resources and then delete that collection.
OK, then POST /posts/batch_deletes with the same entity-body. The sub-resource (a Post::BatchDelete resource) would be created, but would then be immediately run behind-the-scenes and destroyed instantaneously. The effect of running the sub-resource is that all of the posts listed in that sub-resource are also destroyed.
But requiring two requests to delete multiple resources is not restful (specifically, the requirement that it must be this way is not restful). It's just slow because it requires two request-response cycles rather than one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.