Summary
ActiveAdmin’s “Delete” link redirects to the show page instead of deleting the record in Rails 8 applications using Turbo, importmap, and propshaft. This issue arises because Turbo intercepts the link and treats it as a navigatable request, ignoring the data-method="delete" attribute.
Root Cause
- Turbo’s default behavior overrides Rails UJS’s
data-methodhandling, causing DELETE requests to be treated as GET requests. - ActiveAdmin relies on Rails UJS for handling
data-methodattributes, which conflicts with Turbo’s interception mechanism.
Why This Happens in Real Systems
- Turbo’s aggressive interception: Turbo intercepts all links and forms by default, assuming they are navigatable actions.
- Rails UJS and Turbo conflict: Rails UJS and Turbo both attempt to handle
data-methodattributes, leading to inconsistent behavior.
Real-World Impact
- Data integrity risks: Records are not deleted as intended, leading to stale or incorrect data in the system.
- User confusion: Users expect the “Delete” action to remove the record, but instead, they are redirected to the show page.
- Development delays: Engineers spend time debugging and workarounding the issue instead of focusing on core features.
Example or Code
// app/javascript/application.js
import "@hotwired/turbo-rails"
import "controllers"
import Rails from "@rails/ujs"
Rails.start() // Rails UJS is initialized but conflicts with Turbo
How Senior Engineers Fix It
- Use
data-turbo-methodinstead ofdata-method: Turbo recognizesdata-turbo-methodfor non-GET requests. - Disable Turbo for specific links: Add
data-turbo="false"to the delete link to bypass Turbo’s interception. - Update ActiveAdmin configuration: Ensure ActiveAdmin is compatible with Turbo by using the latest version or applying patches.
Example Fix:
actions do
link_to "Remove", resource_path(student), data: { turbo_method: :delete, confirm: "Are you sure?" }
end
Why Juniors Miss It
- Lack of understanding of Turbo’s interception mechanism: Juniors may not realize Turbo overrides Rails UJS behavior.
- Overlooking documentation: Turbo’s documentation on
data-turbo-methodanddata-turboattributes is often missed. - Assuming compatibility: Juniors may assume ActiveAdmin and Turbo work seamlessly without considering underlying conflicts.