如何删除和重定向出涡轮帧



我需要使用标准路由删除一条记录,并重定向到一个单独的页面。链路需要在turbo帧内,并具有确认消息。我正在使用rails 7,并希望以下内容能够起作用;

# _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

按下时,确实会出现确认对话框,并且在确认时,记录会被删除。

然而,帧内容随后消失,导致标准CCD_ 1错误。这向我表明,该系统无法逃脱turbo帧(我认为应该处理turbo_target: "_top".

以下是链接的HTML:

<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>

如何确保用户正确地从框架中重定向到单独的页面?

显示对象的帧标记不同。在原始页面上找不到名为'frame'的标签。我不知道这是否是实际的标签,但它应该是记录,即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

至于为什么转义不起作用,这可能是你重定向的问题。如果它被处理为TURBOSTREAM,它可能不知道该如何处理HTML响应。

因此,你会背诵:<turbo-frame id="frame"> element

尝试

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

最新更新