After attempting to remove an entry from my database by sending an URL via Ajax, I encountered a problem with my JavaScript code:
function disableService(id) {
$.ajax({
type: 'DELETE',
url: '/app/delete_service/',
data: { id: id },
success: function() {
// SOME CODE HERE //
}
});
}
The issue lies in the routes
file, where I have set up a route to manage the request:
delete 'delete_service', to: 'company_services#destroy'
Even after trying to execute the request, it appears that Ajax is manipulating the URL and redirecting to a different one, despite me directly pasting the code into the console for inspection.
A discrepancy can be observed between the code's intended URL and the final requested URL, as seen in the provided screenshot.
https://i.sstatic.net/dcird.png
It's worth noting that while using GET works, POST or DELETE methods do not. Although GET may provide a temporary solution, it is not the optimal approach.
---------- UPDATE -------------
I have also attempted the Rails conventional method:
Routes.rb:
resources :company_services
However, this has resulted in the redirection of the original URL app/company_services/:id
to app/company_services
without the inclusion of :id
.
https://i.sstatic.net/18MkQ.png
Furthermore, here are the routes associated with :company_services
: