【问题标题】:"Are you sure?" confirmation popup not showing for Delete button, Rails 7\“你确定吗?\”Rails 7 的删除按钮未显示确认弹出窗口
【发布时间】:2022-12-16 09:40:21
【问题描述】:

当管理员点击“删除公园”按钮时,我想要一个“你确定吗?”在删除公园之前弹出确认对话框。

我读过很多其他使用 Rails 7 的人通过向表单添加确认消息而获得成功,如下所示:

<% provide(:title, @park.name) %>

<%= render 'park_details' %>

<div>
  <%- if current_user && current_user.admin? %>
    <%= link_to "Edit this park", edit_park_path(@park) %> |
  <% end %>

  <%= link_to "Back to parks", parks_path %>

  <%- if current_user && current_user.admin? %>
    <%= button_to "Delete park", @park, method: :delete,
        class: "btn btn-danger",
        form: { data: { turbo_confirm: "Are you sure?" } } %>
  <% end %>

</div>

以上对我不起作用。公园在没有确认消息的情况下被删除。

表单的 html 呈现如下:

<form data-turbo-confirm="Are you sure?" class="button_to" method="post" action="/en/parks/7"><input type="hidden" name="_method" value="delete" autocomplete="off"><button class="btn btn-danger" type="submit">Delete park</button><input type="hidden" name="authenticity_token" value="XuQsxUyS0LiyYyP_xm1f7XFv9iCkBejRLnSu6DwWOxwQZQVDvkAI_NMRPTuAhLplMbcDZwldwOzmIq_5LqiGnw" autocomplete="off"></form>

此外,如果我使用 link_to 而不是 button_to,公园根本不会被删除(不确定这是否相关但将其包含在描述中以防万一)。

正如有人在这里建议的那样,这可能是 javascript 的问题吗? Ruby on rails: <%= link_to 'Destroy'... doesn't work, but <%= button_to 'Destroy'... does work perfectly

我是初学者,所以不确定要检查什么配置/代码。如果我可以发布更多详细信息来帮助解决此问题,请告诉我。

【问题讨论】:

  • 您好,请查看以下链接。 stackoverflow.com/a/70671361/12111186这对你有帮助
  • 谢谢@AmolMohite 我以前看过这个帖子,卸载并重新安装了 turbo-rails 1.1.1,但它没有解决问题。

标签: ruby-on-rails ruby-on-rails-7 turbo button-to rails7


【解决方案1】:

尝试使用 javascript

$(document).on 'click', '.park', ->
  if confirm('Are you sure?')
    delete_park($(this).data('url'))
  return



  <%- if current_user && current_user.admin? %>
    <%= button_to "Delete park", @park, method: :delete,
        class: "btn btn-danger del_park",
        form: { data: { turbo_confirm: "Are you sure?" } } %>
  <% end %>

【讨论】:

  • 在哪里添加javascript?当我将它添加到视图文件时,它只是呈现为文本。我是初学者,感谢您的耐心等待。
  • 在 app>assets>javascripts>application.js 添加 javascript
  • 您的答案可以通过其他支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写出好的答案的信息in the help center
【解决方案2】:

按照 michael-reeves 的建议,通过重新安装 Turbo 解决了这个问题:https://github.com/rails/rails/issues/44170

$ rails importmap:安装

$ rails turbo:install 刺激:安装

【讨论】:

  • 如果我使用顺风,没有得到 rails 弹出确认是否正常?
【解决方案3】:
<%= link_to fa_icon('trash'), @blog, data: { turbo_method: :delete,
                          turbo_confirm: "Are you sure you want to delete this post" } %>

这对我有用。弹出确认消息和删除链接。

这是销毁的控制器:

def destroy
    @blog.destroy

    respond_to do |format|
      format.html { redirect_to blogs_url, status: :see_other }
    end
  end

“turbo_method: :delete”和“status: :see_other”是关键部分。对不起,如果我迟到了回答这个问题,但这在我的 rail 7 应用程序上对我有用。在我终于克服了对“button_to”的需求之前,我花了很多时间谷歌搜索。我希望这可以帮助别人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-20
    • 2012-07-21
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多