How to delete and redirect out of a turbo frame

Viewed 15

I need to delete a record using standard routing and redirect to a separate page. The link needs to be within a turbo frame and have a confirmation message. I'm using rails 7 and would expect the following to work;

# _form.html.slim

<%= turbo_frame_tag 'frame' do %>
<%= link_to "Delete", url_for(writable), data: { turbo_target: "_top", turbo_method: :delete, turbo_confirm: 'Are you sure?' }, class: "button" %>
<% end %>
# records_controller.rb

def destroy
    @record = Record.find(params[:id])

    @record.destroy

    redirect_to records_path, status: :see_other
end

When pressed, the confirm dialog does appear and, when confirmed, the record is deleted.

However the frame content then disappears resulting in the standard turbo.es2017-esm.js:3060 Response has no matching <turbo-frame id="frame"> element error. This suggests to me that the system isn't able to escape the turbo frame (which I thought would be handled turbo_target: "_top".

Here is the rendered HTML of the link:

<a data-turbo-target="_top" data-turbo-method="delete" data-turbo-confirm="Are you sure?" class="button" href="/events/aff7ec0e-eeea-4d5c-bd4a-f81f79b70c6b">Delete</a>

How can I ensure that the user is correctly redirected out of the frame and to a separate page?

0 Answers
Related