Deleting multiple contacts using check_box_tag

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

<% @contacts.each do |contact| %>

<td ><%= check_box_tag "contact_ids[]", contact.id %> </td>

<td><%= contact.name %></td>

<td><%= contact.phone %></td>

<td><%= contact.mailid %></td>

<td><%= contact.age %></td>

<td><%= link_to 'Show',

                contact,

                :remote=>true                   

                     %></td>

<td><%= link_to 'Edit',

                    edit_contact_path(contact),

                    :remote => true%></td>

<td><%= link_to 'Destroy',

                contact,

                :method => :delete,

                :data => { :confirm => 'Are you sure?' },

                :remote => true %></td>

<td><%= link_to 'Send Mail',

                {:action => "createmail", :id => contact.id},

                :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”.

Where am I doing wrong? Can anyone help.

But it is not working.

A useless statement. What does that mean?

but my development log says no value has been recieved in "id".

Where am I doing wrong?

Post the lines from your log showing the request being submitted and the *actual* error message.

This is what I am receiving in the log,

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)

So that tells you what's wrong with your form, eh? :slight_smile:

I am aware of that. But I am not able to figure out the way I should pass the parameters. I need help with that.

this works fine for me: http://www.skuunk.com/2008/05/checkbox-arrays-in-rails.html

:slight_smile:

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 :slight_smile:

> 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.

HTH,

Hi,

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)

Regards Sumit Srivastava

The power of imagination makes us infinite…

This is my code for the button to submit this delete request,

<%= link_to ‘Destroy Selected’,

            {:action => 'destroySelected',
            :contact_ids => 'contact_ids[]'},
            :remote => true%>

Where destroySelected is the action in controller to delete these.

Code for the destroySelected action is,

def destroySelected Contact.delete_all(:id => params[:id])

respond_to do |format|
    format.html {redirect_to contacts_url}
    #format.js
end

end

Regards Sumit Srivastava

The power of imagination makes us infinite…