Fix destroy button click navigating to show page on index#3026
Open
55728 wants to merge 1 commit intothoughtbot:mainfrom
Open
Fix destroy button click navigating to show page on index#302655728 wants to merge 1 commit intothoughtbot:mainfrom
55728 wants to merge 1 commit intothoughtbot:mainfrom
Conversation
Clicking the Destroy button on the index collection table was triggering row navigation to the show page. The visitDataUrl Stimulus action only checked event.target.href to skip navigation, but button_to generates a <button> element without href. Replace the href check with event.target.closest() to properly detect interactive elements (links, buttons, submit inputs) that should not trigger row navigation. Fixes thoughtbot#2978
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #2978
Clicking the "Destroy" button on the index page collection table
was navigating to the resource's show page instead of triggering
the delete action.
Why
The
visitDataUrlaction intable_controller.jscheckedevent.target.hrefto decide whether to skip row navigation.Since
button_togenerates a<form>with a<button>(whichhas no
hrefattribute), the check was bypassed, causingTurbo.visit(dataUrl)to fire.How
Replaced
event.target.hrefwithevent.target.closest("a[href], button, input[type='submit']")to properly detect interactive elements that should not trigger
row navigation. This also handles:
<button>elements (the reported bug)<input type="submit">elements (future safety)Added a feature spec that verifies dismissing the destroy
confirmation keeps the user on the index page.
Changed files
app/assets/javascripts/administrate/controllers/table_controller.js(1 line)spec/features/orders_index_spec.rb(+11 lines)