Datsik
Datsik

Reputation: 14824

Trying to figure out Ruby on Rails remote: true callbacks

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.

Upvotes: 25

Views: 23647

Answers (5)

cdmo
cdmo

Reputation: 1309

You don't have to use jQuery. Could do:

document.querySelector('#reportform')
    .addEventListener("ajax:success", function(data, status, xhr) {
        document.querySelector('#reportform').text('Done.');
    });
    .addEventListener("ajax:error", function(xhr, status, error) {
        document.querySelector('#reportform').text('Failed.');
    });

Upvotes: 0

Yi Feng Xie
Yi Feng Xie

Reputation: 5017

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;
});

Upvotes: 9

sparkle
sparkle

Reputation: 7390

Turbolinks compatible

<script type="text/javascript">

    $(document).on('ajax:success', 'a[data-remote].watching', function(e, data, status, xhr){

    });

</script>

Upvotes: 4

Victor BV
Victor BV

Reputation: 1101

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.

Upvotes: 43

Rahul Tapali
Rahul Tapali

Reputation: 10137

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>

Upvotes: 3

Related Questions