0

我正在轮询一个长时间运行的进程以了解 javascript 中的成功/失败。如果该过程失败,我需要它导航到特定页面并显示flash['alert']. 我正在使用 导航到一个新页面window.location.href,但我不知道如何flash['alert']从 js 创建一个。作为一个完整的临时破解,我将错误消息作为 url 参数传递。然后在我的控制器new操作中,我检查参数并创建flash['alert']是否存在错误。我不喜欢这个解决方案,我假设有一种更清洁的方法可以做到这一点。

function checkReportStatus() {
  var report_id = $(".report").data("report-id");
  $.get('/reports/' + report_id + '/check_report_status', function(report_status) {
      if (report_status === 'completed') {
        location.reload();
      }
      else if (report_status === 'error' || report_status === 'failed') {
        window.location.href = "new?error=ERROR_MESSAGE";
      }
      else {
        console.log("Report generation in progress...");
        setTimeout(checkReportStatus, 3000);
      }
  });
}
4

2 回答 2

2

我通过将check_report_status代码移动到控制器操作并使用render而不是redirect_to.

def check_report_status
    @report = Report.find(params[:id])
    flash[:error] = "Report error"
    render :js => "window.location = 'PATH'"
  end
于 2013-08-29T17:52:05.203 回答
1

$('element').effect('highlight', {color:'#XXXXXX'},1500);产生不错的闪烁效果,因为您确实在标签中提到了 jQ 的使用。

于 2013-08-27T20:39:05.773 回答