user3448806 user3448806 - 1 month ago 10
jQuery Question

Detect server down when submit remote: true in Rails

Can I detect when client couldn't send request to server ? (server down or broken internet link or something ...)

Exp. when I submit form or click link with

remote: true
, if request couldn't come to server, I could show a message to user.

<%= form_for @item, remote: true do %>
....
<% end %>

or

<%= link_to @item, remote: true %>

Answer

You can write AJAX callbacks for remote: true requests like below.

<script>
  $(document).ready(function(){
    $('#item').on('ajax:success', function(e, data, status, xhr) {
      //Success action
    }).on('ajax:error',function(e, xhr, status, error){
      //Failure action
    });
  });
</script>

You can write whatever you want in the error block.

Ref

Comments