Trying to figure out Ruby on Rails remote: true callbacks
Try this:
Put your javascript code on document ready
:
<script>
$(document).ready(function(){
$('#reportform')
.bind("ajax:success", function(data, status, xhr) {
$('#reportalert').text('Done.');
});
.bind("ajax:error", function(xhr, status, error) {
$('#reportalert').text('Failed.');
});
})
</script>
Turbolinks compatible
<script type="text/javascript">
$(document).on('ajax:success', 'a[data-remote].watching', function(e, data, status, xhr){
});
</script>
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;
});
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.