我一直在与Rails 4和UJS作斗争,并注意到Rails 4.1的行为(特别是Rails - UJS)不像我期望的那样。
当使用带有确认对话框的远程链接时,UJS驱动程序在确认弹出框被批准之前发送XHR(使确认弹出框毫无意义)。我本来希望Rails能够处理这种选项组合,但是我不得不编写自己的javascript来执行确认弹出并挂钩到"ajax: beforeend"事件,以防止请求过早触发。
这是我没有工作的(请求立即触发,没有等待确认对话框返回true):
= link_to 'Ajax Button', app_path(resource), method: :delete,
remote: true, id: 'ajax_link', disable_with: 'Wait...',
confirm: 'Are you sure?'
这是我现在的工作:
= link_to 'Ajax Button', app_path(resource), method: :delete,
remote: true, id: 'ajax_link', disable_with: 'Wait...'
$ ->
$("a[data-remote]#ajax_link").on 'ajax:beforeSend', (xhr, settings) ->
return confirm("Are you sure?")
这只是我还是不应该rails/jquery-ujs处理上述选项的组合,并等待确认弹出?在过去,Rails 3和更早的版本,当涉及到AJAX和远程请求时,远程链接/按钮通常"只是工作",但是在Rails 4中,使用AJAX似乎需要比以前更多的技巧。
有人在Rails 4.1中遇到过这种情况吗?还有其他的解决方案吗?
下面应该可以工作
= link_to 'Ajax Button', app_path(resource), method: :delete, remote: true, id: 'ajax_link', data: { disable_with: "Wait..." }, data: { confirm: 'Are you sure?' }