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

                     %></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”.

Where am I doing wrong? Can anyone help.

On Thu, Jul 19, 2012 at 12:37 AM, Sumit S.
[email protected] wrote:

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.


Hassan S. ------------------------ [email protected]

twitter: @hassan

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)

On Fri, Jul 20, 2012 at 2:11 AM, Sumit S.
[email protected] wrote:

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


Hassan S. ------------------------ [email protected]

twitter: @hassan

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:

:slight_smile:

2012/7/20 Sumit S. [email protected]

On Fri, Jul 20, 2012 at 10:43 AM, Sumit S.
[email protected] wrote:

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,

Hassan S. ------------------------ [email protected]

twitter: @hassan

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

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

The power of imagination makes us infinite…