I'm working on an application that allows user to select multiple entries. Once those entries are selected, the user can choose to delete them all. When clicking on the delete button (after selected one or more entries), a modal dialog window is displayed showing the entries that the user has selected with a confirmation button.
Currently, all of the above is done using jQuery on the clientside. I've managed to get it all set so that the confirmation button (which is a link_to
using the :method => :delete
) has a URL of /entries/12,13,16,17
where the numbers represent the id's of the entries that the user selected. I've also tried sending the url with the format of /entries/[1,2,3,4]
with no luck. I have a feeling I'm not sending a real array.
Here's my delete method, which works perfectly fine when one id is used (i.e. /entries/1
), and would hopefully continue to work with one id:
def destroy
@entry = current_user.entries.find(params[:id])
@entry.destroy
respond_to do |format|
format.html { redirect_to(entries_url) }
format.xml { head :ok }
end
end
When I send multiple id's (in the format of 1,2,3,4,5
), only the first entry is deleted, while the rest are left untouched. Here is what my webrick log looks like after performing the action:
Processing EntriesController#destroy (for 127.0.0.1 at 2009-12-03 23:07:24) [DELETE]
Parameters: {"action"=>"destroy", "_method"=>"delete", "id"=>"19,22", "controller"=>"entries"}
User Load (0.3ms) SELECT * FROM "users" WHERE ("users"."id" = '2') LIMIT 1
User Update (0.3ms) UPDATE "users" SET "updated_at" = '2009-12-04 04:07:24', "last_request_at" = '2009-12-04 04:07:24' WHERE "id" = 2
Entry Load (0.2ms) SELECT * FROM "entries" WHERE ("entries"."id" = 19 AND ("entries".user_id = 2))
Tagging Load (0.4ms) SELECT * FROM "taggings" WHERE ("taggings".taggable_id = 19 AND "taggings".taggable_type = 'Entry' AND (taggings.context = 'tags'))
Tag Load (0.2ms) SELECT * FROM "tags" WHERE ("tags"."id" IN (22,23,24,29))
Tagging Destroy (0.2ms) DELETE FROM "taggings" WHERE "id" = 66
Tagging Destroy (0.1ms) DELETE FROM "taggings" WHERE "id" = 67
Tagging Destroy (0.0ms) DELETE FROM "taggings" WHERE "id" = 68
Tagging Destroy (0.1ms) DELETE FROM "taggings" WHERE "id" = 69
Tagging Load (0.1ms) SELECT * FROM "taggings" WHERE ("taggings".taggable_id = 19 AND "taggings".taggable_type = 'Entry')
Entry Destroy (0.1ms) DELETE FROM "entries" WHERE "id" = 19
Entry Load (0.5ms) SELECT * FROM "entries" WHERE ("entries".user_id = 2)
Redirected to http://localhost:3000/entries
As you can see, the ids are sent ("id"=>"19,22"
) but only the first entry is loaded. After that, its associated taggings are deleted (I'm using the acts_as_taggable_on
plugin). Then, the first entry (id => 19
) is destroyed, and the user is redirected back to the entries listing page.
What is should do is repeat that process (up the the redirect) for each id passed. I'm confused, because the RoRs docs state that:
id - Can be either an Integer or an Array of Integers.
What am I doing wrong? I feel like I'm so close, I can taste it! It was a huge triumph for me to get all the jQuery working, so now I know the only piece missing in the controller method handling the multiple id's.
Any help would be greatly appreciated!