You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Administrate 0.16.0+Rails6.1:含span标签的link_to删除功能失效

问题:Administrate删除按钮触发GET请求而非DELETE请求

环境

  • Administrate gem 0.16.0
  • Rails 6.1.3.1

症状

在记录列表中点击带垃圾桶图标的删除按钮时,本应发送DELETE请求执行destroy动作,但实际发送GET请求跳转到了show页面。

  • 移除data: { confirm: ... }弹窗配置,或仅保留文本“delete”而非图标span标签时,删除功能正常。
  • 已确认rails-ujs已包含在application.js中,尝试改用button_to也无效。

相关代码

ERB模板代码

<td><% if valid_action? :destroy, collection_presenter.resource_name %><%= link_to(
    [namespace, resource],
    class: "text-color-red",
    method: :delete,
    data: { confirm: "This alert contains #{resource.alert_details.count} alert details. Are you sure you want to delete it?" },
            title: "Delete",
 ) do %>
     <span class="material-icons">
       delete
     </span>
 <% end if show_action? :destroy, resource %>
<% end %></td>

渲染后的HTML

<a class="text-color-red" data-confirm="This alert contains 0 alert details. Are you sure you want to delete it?" data-turbo-method="delete" title="Delete" rel="nofollow" data-method="delete" href="/admin/alerts/67">
            <span class="material-icons">
              delete
            </span>
</a>

解决方案

问题根源是Rails 6.1默认集成的Turbo与rails-ujs的事件处理冲突:渲染后的HTML同时存在data-turbo-method和data-method属性,带confirm弹窗时两者的监听逻辑互相干扰,导致DELETE请求未被正确触发。

只需在link_to的data选项中添加turbo: false,禁用该链接的Turbo处理,让rails-ujs单独负责DELETE请求和确认弹窗:

<td>
  <% if valid_action? :destroy, collection_presenter.resource_name %>
    <%= link_to(
      [namespace, resource],
      class: "text-color-red",
      method: :delete,
      data: { 
        confirm: "This alert contains #{resource.alert_details.count} alert details. Are you sure you want to delete it?",
        turbo: false
      },
      title: "Delete",
    ) do %>
      <span class="material-icons">
        delete
      </span>
    <% end if show_action? :destroy, resource %>
  <% end %>
</td>

修改后,点击按钮会先弹出确认弹窗,确认后发送DELETE请求执行destroy动作,功能恢复正常。

内容的提问来源于stack exchange,提问作者Pratha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:12:40