I have build a contacts page which displays a number of contacts. I have
attached a check_box_tag with each row of these. And then a button to
delete those selected. But it is not working. Following is the code
snipet,
_contact.html.erb
%></td>
:remote => true %></td>
<% end %>
<%= link_to ‘Destroy Selected’,
{:action => 'destroySelected',
:id => 'contact_ids'},
:remote => true%>
In Contacts_controller
def destroySelected
Contact.delete_all(:id => params[:contact_ids])
respond_to do |format|
format.html {redirect_to contacts_url}
format.js
end
end
but my development log says no value has been recieved in “id”.
Started GET “/destroy_selected?id=contact_ids” for 127.0.0.1 at Fri Jul
20
14:32:34 +0530 2012
Processing by ContactsController#destroySelected as JS
Parameters: {“id”=>“contact_ids”}
SQL (0.1ms) DELETE FROM “contacts” WHERE “contacts”.“id” IS NULL
Rendered contacts/destroySelected.js.erb (0.0ms)
Completed 200 OK in 2ms (Views: 1.2ms | ActiveRecord: 0.1ms)
Started GET “/destroy_selected?id=contact_ids” for 127.0.0.1 at Fri Jul 20
14:32:34 +0530 2012
Processing by ContactsController#destroySelected as JS
Parameters: {“id”=>“contact_ids”}
SQL (0.1ms) DELETE FROM “contacts” WHERE “contacts”.“id” IS NULL
So that tells you what’s wrong with your form, eh?
I am aware of that. But I am not able to figure out the way I should pass
the parameters.
First, you should strongly consider the wisdom of having a GET
request deleting any resource, let alone multiple ones
Started GET “/destroy_selected?id=contact_ids” for 127.0.0.1 at Fri Jul
Then I would look at my form and determine whether all the check
boxes are correct. Then write some unobtrusive JS to collect those
contact ids into an array. Then you can have the JS submit that
array to your controller method (NOT using GET!) , which will have
to be rewritten to accept an array.
Still nothing has changed. The log shows parameters being passed as,
Parameters: {“contact_ids”=>“contact_ids[]”}
Complete log is
Started GET “/destroy_selected?contact_ids=contact_ids%5B%5D” for
127.0.0.1
at Mon Jul 23 14:56:04 +0530 2012
Processing by ContactsController#destroySelected as JS
Parameters: {“contact_ids”=>“contact_ids[]”}
SQL (0.1ms) DELETE FROM “contacts” WHERE “contacts”.“id” IS NULL
Redirected to http://localhost:3000/contacts
Completed 302 Found in 1ms (ActiveRecord: 0.1ms)