How to delete multiple records in rails by passing the multiple values to REST api?. My current routes looks like this:
products DELETE /products/:id(.:format) products#destroy
When I try to pass multiple values as as an array (DELETE /products/[ids]), it says the routes doesn't exists.
REST stands for REpresentational State Transfer and describes resources (in our case URLs) on which we can perform actions. CRUD , which stands for Create, Read, Update, Delete, are the actions that we perform. Although, in Rails, REST and CRUD are bestest buddies, the two can work fine on their own.
If you're doing that pass an array of ids and use destroy_all
Product.where(id: params[:ids]).destroy_all
After a lot research, I have figured out a way to delete multiple records by passing the ids as a comma separated values to REST api.
You have call the API like this
DELETE /products/id1,id2,id3
Now handle the business logic in your controller
Product.where(id: params[:id].split(',')).destroy_all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With