I am trying to display an error message to the user saying a booking cannot be cancelled if something goes wrong.
On success ajax intercepts the normal form behaviour with remote: true and renders a partial (incidentally within an existing partial), but I can't bind an 'ajax:error' event to my form in my js.erb file.
I've tried lots and lots of different things and syntax variations to try to get it to work. (I am quite new to javascript/jQuery so it could be something basic as js.erb doesn't show errors in the console). This was my latest attempt:
cancel_success.js.erb:
$('#cancel-message').html("<%= j (render 'cancel_success') %>");
$('#cancel-message').slideDown(350);
$('#search-show-container').hide()
$('#reservationId').closest('form').on('ajax:error', function(xhr, status, error) {
$("#cancel_error_text").html("Sorry, we can't cancel your booking at the moment, please try calling the restaurant:");
});
My form view:
<p id="cancel_error_text"></p>
<div id="search-show-container">
<%= form_tag(cancel_success_searches_path, remote: true) do %>
<%#= label_tag 'Reservation ID' %>
<%= text_field_tag :reservationId %>
<%= submit_tag 'Cancel Booking', name: nil %>
<% end %>
</div>