So I've been googling on how to set them up, I ended up with this code in the end.
<script>
$('#reportform')
.bind("ajax:success", function(data, status, xhr) {
$('#reportalert').text('Done.');
});
.bind("ajax:error", function(xhr, status, error) {
$('#reportalert').text('Failed.');
});
</script>
<h2>Review Driver</h2>
<p>Fill out your review of the driver</p>
<div class="hero-unit form-signin" id="reportformdiv">
<%= form_for(@report, html: { id: "reportform" }, remote: true, update:
{ success: "response", failure: "error"} ) do |t| %>
<p id="reportalert"></p>
<%= t.text_field :plant_site, placeholder: "Plant Site" %>
<%= t.text_field :route_number, placeholder: "Route Number" %>
<%= t.text_field :driver_name, placeholder: "Driver name if available" %>
<%= t.date_select :date_recorded, html: { class: "input-block-level" } %>
<%= t.text_field :action, placeholder: "Action taken" %>
<%= t.text_area :report_body, placeholder: "What you witnessed",
style: "height: 300px;",
class: "input-block-level" %>
<%= t.submit "File Report", class: "btn btn-primary btn-large" %>
<% end %>
</div>
But it is not working and I have no idea why, I'm sure I've done something wrong, I'm new to RoR and I love the fact that I can declare this remote: true in the form its self, once I figure out how to set the callbacks I'll be good to go :) Thanks in advance.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With