Delete multiple objects in Rails using a RESTful controller?

I want to delete several objects of the same type using a RESTful controller.

The simplest thing that I can think of is to force to destroy an action that expects to destroy a list of objects destroyed by commas.

Is there a more elegant way to do this?

+5
source share
3 answers

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

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

+3
source

You can use nested forms for it.

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

, ...

<% 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 %>
+2

RESTful.

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

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

, GET, PUT DELETE REST, POST . , , URL-, , , () . POST , GET, PUT DELETE .

POST "", . POST , , URL- . URL- , PUT.

-1
source

All Articles