[Chartjs]-How to loading animation while populating activerecord in Rails 4.0?

2👍

There is no simple way to make view interact with controller, because view is being executed on client side, but controller is executed on server’s.

What you could do is to use some jQuery code to show animation after user press the button, but before next document is loaded (without any progress being shown).
Put div with animation element (example) to you submit page like this:

<div class="progress">
some .gif here
</div>
<div class="your_content">
all other code
</div>

Add code like this to your application.js file

function showProgress() {
  $('.your_content').hide();
  $('.progress').fadeIn("slow");
}

$(document).ready(function () {
  $('.progress').hide();        //hide gif on page load    
  $('.your_button_class').on('click', showProgress); //and show it after clicking your button
});

Other working solutions will include relatively complicated AJAX code.
Also consider using caching for both – model and view.

1👍

Perhaps you could use a DelayedJob and use long-polling or interval polling to determine if the job is finished. Once the job is finished it is removed from the DelayedJobs table so all you need to do is check if it exists. On the client-side you can initially show the animation then when the long-polling or interval polling response says that it is finished, disable the animation and show the chart.

Leave a comment