为什么确认rails button_to helper的选项不起作用

时间:2016-01-27 16:53:37

标签: ruby-on-rails

我有一个rails button_to,我希望它在继续之前显示一个确认对话框。

<%= button_to("Rebuild indexes", action: "rebuild_indexes", data:
{confirm: "Are you sure you want to reset the indexes?" }) %>

行动正在发生,但我没有收到确认框。我读到这个问题可能与javascript有关。我以为我已经启用了。

application.js
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require turbolinks
//= require tinymce-jquery
//= require ckeditor/init
//= require ckeditor/config.js
//= require jquery.are-you-sure
//= require script
//= require script.responsive
//= require_tree .

如果我对按钮进行检查,我会看到

<form class="button_to" method="post" action="/help/rebuild_indexes?data%5Bconfirm%5D=Are+you+sure+you+want+to+reset+the+indexes%3F">
  <input type="submit" value="Rebuild indexes">
  <input type="hidden" name="authenticity_token" value="3mnAMmyHaINiDngjQn87EMmhmetp2VJcX+lwcmbUlhwBfv7V1hpLc9ZY1OF5JAFm9erFJjX+qyDz35/KK41jaA==">
</form>

出现确认框时我错过了什么?

5 个答案:

答案 0 :(得分:2)

data: {confirm: "<confirm message>"}被添加到url参数hash而不是作为第三个参数传递 - 尝试分离像这样的哈希:

button_to("Rebuild indexes", {action: "rebuild_indexes"}, data: {confirm: "Are you sure you want to reset the indexes?" })

答案 1 :(得分:2)

我遇到了类似的问题:

 <%= button_tag 'Delete', onclick: "location.href = #{polymorphic_url(obj)}", :method => :delete, type: 'button', class: 'btn btn-danger', title: 'Delete', id: 'delete_btn', data: { confirm: 'Are you sure you want to delete this record?' } %>

删除“类型:'按钮'”修复了确认提示问题

答案 2 :(得分:1)

尝试添加link_to而不是button_to。 '/ indexes / rebuild'是路线。

<%= link_to "Rebuild indexes", '/indexes/rebuild', data: { confirm: 'Are you sure you want to reset the indexes?' }, class:"btn btn-default" %>

答案 3 :(得分:1)

<%= button_to "/home/delete?cardId="+card.id.to_s, data: { confirm:'Are you sure you want to delete?' }  do %>
    <i class="fa fa-times"></i>
<% end%>

此生成

<form class="button_to" method="post" action="/home/delete?cardId=15">
 <button data-confirm="Are you sure you want to delete?" type="submit">
    <i class="fa fa-times"></i>
 </button>
</form>

答案 4 :(得分:0)

这是Turbolink功能的已知问题。您需要通过在数据属性中插入“ turbolinks:false”来显式关闭helper方法的turbolink。

<%= button_to("Rebuild indexes", action: "rebuild_indexes", data:
{turbolinks: false, confirm: "Are you sure you want to reset the indexes?" }) %>