javascript - How to send id from controller to js in Rails app -


i need generate big report file in background. here simple view create orderreport object.

<%= simple_form_for order_report, remote: true |f| %>   <%= f.input :start_date, as: :date, html5: true %>   <%= f.input :end_date, as: :date, html5: true %>   <%= f.submit "generate report", id: "test" %> <% end %> 

and going on in controller:

def create    order_report = orderreport.new(order_report_params)    order_report.user = current_user    order_report.save     orderreportjob.new(order_report).delay.perform     render nothing: true end 

after user click submit button action creates background process generate report. wrote endpoint check status of background job. js onclick function submit buttom id #test

$.ajax({       url: report_url,       success: function(report) {         if(report.status === 'progress') {           $("#spin").show();           $interval = setinterval(checkstatus, 3000);         }       }     }); 

this part of js script. works good, final step send id of created orderreport js file. can see in js script have variable report_url - it's hardcoded , looks like

var report_url = '/order_reports/1' 

so main idea catch id of created orderreport, if it's possible, , use in js script. how can pass correctly?

update:

order_report.js

$(function () {   $('#test').click(function() {     var report_url = '/order_reports/39'      $.ajax({       url: report_url,       success: function(report) {         if(report.status === 'progress') {           $interval = setinterval(checkstatus, 3000);         }       }     });      function checkstatus() {       $.ajax({         url: report_url,         success: function(report) {           if(report.status === 'done') {             clearinterval($interval)           }         }       });     }   }); }); 

a more restful solution use meaningful response codes tell client happened request:

def create    order_report = orderreport.new(order_report_params)    order_report.user = current_user     respond_to |format|      if order_report.save        orderreportjob.new(order_report).delay.perform        format.json { head :created, location: order_report }      else        format.json { head :unprocessable_entity }      end    end end 

head :created, location: order_report returns 201 - created response location header contains url created resource.

this lets listen rails ujs ajax:success , ajax:error events:

<%= simple_form_for order_report, remote: true, html: { class: 'order_report_form', 'data-type' => 'json'} |f| %>   <%= f.input :start_date, as: :date, html5: true %>   <%= f.input :end_date, as: :date, html5: true %>   <%= f.submit "generate report", id: "test" %> <% end %> 

$(document).on('ajax:success', '.order_report_form', function(e, data, status, xhr){   function checkstatus(url) {     return $.getjson(url).then(function(data) {       // logic here test if have desired result       if (!desiredresult) {         // never use setinterval ajax not care         // if previous request done.         // instead use settimeout recursion         settimeout(1000, function(){ checkstatus(url) });       } else {         // awesome       }     }   }   checkstatus(xhr.getresponseheader('location')); });  $(document).on('ajax:error', '.order_report_form', function(){   alert("oops"); }); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -