Simon Knight Simon Knight - 5 months ago 13
Ajax Question

Ajax request to rails controller is setting the id parameter = "destroy"?

so I have a weird problem where for some reason the id parameter in my delete request is being set to "destroy". Here is the code for my ajax request

function deleteItems(id_data, table){
$.ajax({
url: '/items/destroy',
method: 'DELETE',
processData: true,
data: {ids: id_data },
success: function(){
dropItems(id_data, table)
}
});
}


And here are the parameters I am getting in the rails controller

{"ids"=>["6", "19"], "controller"=>"items", "action"=>"destroy", "id"=>"destroy"}


If I try and set the data key to id (instead of id's) I get this

{"id"=>"destroy", "controller"=>"items", "action"=>"destroy"}


Any help in figuring out why id is being set to "destroy" would be awesome. Thanks

Answer

It seems you're using resource routing. In that case your url should look like

url: '/items/:id'

That url will destroy only one item. For example:

url: '/items/9

Or you need to define new route for

items/destroy

and pass ids as a parameters.