How do you get a turbo_confirm dialog to happen before a Stimulus action is called?

I have this type of set up:

<%= button_to destroy_all_foo_path(@foo), method: :delete,
          form: { data: { action: "click->bar#baz", turbo_confirm: "..." } }," do %>

I’ve also tried swapping the order of the action and turbo_confirm properties, it makes no difference. I also tried using submit-> instead of click-> and it’s the same result.

Functionality wise everything works independently. The ok / cancel dialog comes up and my StimulusJS action fires.

The problem is, the Stimulus action fires right as the confirm dialoog box is opened before the user hits ok or cancel. The expected behavior is the Stimulus action does not get called unless the user hits ok.

How can you guarantee this works in the correct order?

Technically you can remove the turbo_confirm and handle calling confirm() manually in your Stimulus action and only run it if the result is true but I was hoping to use turbo_confirm.

1 Like