Deleting multiple contacts using check_box_tag

59 views
Skip to first unread message

Sumit Srivastava

unread,
Jul 19, 2012, 3:37:13 AM7/19/12
to rubyonra...@googlegroups.com
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| %>
  <tr class='contactrow' id='contact-<%=contact.id%>'>
    <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>
  </tr>
<% 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.
 

Hassan Schroeder

unread,
Jul 19, 2012, 10:27:39 AM7/19/12
to rubyonra...@googlegroups.com
On Thu, Jul 19, 2012 at 12:37 AM, Sumit Srivastava
<sumit.the...@gmail.com> 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 Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Sumit Srivastava

unread,
Jul 20, 2012, 5:11:12 AM7/20/12
to rubyonra...@googlegroups.com
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)

Hassan Schroeder

unread,
Jul 20, 2012, 9:58:19 AM7/20/12
to rubyonra...@googlegroups.com
On Fri, Jul 20, 2012 at 2:11 AM, Sumit Srivastava
<sumit.the...@gmail.com> 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? :-)

Sumit Srivastava

unread,
Jul 20, 2012, 1:43:19 PM7/20/12
to rubyonra...@googlegroups.com
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.

Hassan Schroeder

unread,
Jul 20, 2012, 2:54:26 PM7/20/12
to rubyonra...@googlegroups.com
On Fri, Jul 20, 2012 at 10:43 AM, Sumit Srivastava
<sumit.the...@gmail.com> 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 :-)

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

Carlos Eduardo Ribeiro

unread,
Jul 20, 2012, 2:12:19 PM7/20/12
to rubyonra...@googlegroups.com
this works fine for me:  http://www.skuunk.com/2008/05/checkbox-arrays-in-rails.html 
:)

2012/7/20 Sumit Srivastava <sumit.the...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/DWnPNOCF_jEJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

sumit srivastava

unread,
Jul 23, 2012, 5:26:39 AM7/23/12
to rubyonra...@googlegroups.com
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...

sumit srivastava

unread,
Jul 23, 2012, 5:49:08 AM7/23/12
to rubyonra...@googlegroups.com
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...


Reply all
Reply to author
Forward
0 new messages