Trying to figure out Ruby on Rails remote: true callbacks Trying to figure out Ruby on Rails remote: true callbacks ajax ajax

Trying to figure out Ruby on Rails remote: true callbacks


According to Rails' wiki, the code bellow should work:

<script>  $(document).ready(function(){    $('#reportform').on('ajax:success', function(e, data, status, xhr){      $('#reportalert').text('Done.');    }).on('ajax:error',function(e, xhr, status, error){      $('#reportalert').text('Failed.');    });  });</script>

A similar code worked for me in Rails 3.2.14 and jquery-rails 3.0.4

Hope it helps.


Since Rails 5.1, response, status, and xhr must be extracted through event.detail see: https://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers

This is one possible solution:

$(document).on('ajax:success', '#reportform', event => {  const [response, status, xhr] = event.detail;});


Turbolinks compatible

<script type="text/javascript">    $(document).on('ajax:success', 'a[data-remote].watching', function(e, data, status, xhr){    });</script>