4

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?

JesseWelch92
  • 109
  • 10
  • 1
    i'm fighting exactly the same problem. did you find anything in the mean time? it should be supported but it doesn't seem to work? – user2166514 Nov 04 '22 at 06:23
  • `data-turbo-target = _top` works for me ``` <%= link_to( "Delete", @blog_post, data: { turbo_target: "_top", turbo_method: :delete, turbo_confirm: 'Are you sure?' } ) %> ``` – gonzohunter Apr 12 '23 at 15:34

1 Answers1

0

Your frame tag from which the object was displayed is different. It can't find the tag named 'frame' on the original page. I don't know if that is the actual tag but it should be the record i.e. turbo_frame_tag record do ...

_form.html.erb

turbo_frame_tag 'frame' do <-- frame is probably record_22 or something
  record.attribute_1
  record.attribute_2
  etc...
  link_to delete, delete_path, data: { turbo_frame: '_top', ....}
end

As far as why the escape is not working that could be an issue with your redirect. If it is being processed as a TURBOSTREAM it could just not know what to do with a HTML response.

Hence why you recive: <turbo-frame id="frame"> element

Try

respond_to do | format |
if @record.destroy
  flash[:success] = t(".success")
  format.html { recede_or_redirect_to records_path }
end
Chrismisballs
  • 167
  • 12