Rails 5 js.erb奇怪的行为

时间:2019-05-28 05:56:16

标签: javascript ajax ruby-on-rails-5

最近我遇到了一个奇怪的问题。这是我的js.erb

<% if @job.present? %>
  $('.process .status-message').text( "Please do no refersh page. File uploading status: 0%" )
  var checkStatus = setInterval(function() {
    $.ajax({
      url: '/seisankanri/check_upload_progress/' + '<%= @job.job_id %>',
      success: function(progress_status) {
        $('.process .status-message').text( "Please do no refersh page. File uploading status: "+progress_status.status+"%" )
        if(progress_status.status == 100){
          clearInterval(checkStatus)

          $('.process').addClass("spinner_hide");
          $('body').removeClass("no_scroll")
          $(".model-view").html("<%= escape_javascript(render(partial: 'ro_production_import_model', locals: {message: I18n.t('templates.ro_file_upload.file_upload_success'), job_id: '', error_logs: ''} )) %>");
          $('#message-model').show();
          $('.close-model').click(function(){
            $('#message-model').hide();     
          })
          $('#file').val('');
          $('.error_link').click(function(event){
           $('.error_block').removeClass("error_messages_block");
           event.preventDefault();
          });
        } else if(progress_status.error_job_id.length > 0) {

          $('.process').addClass("spinner_hide");
          $('body').removeClass("no_scroll")

          $(".model-view").html("<%= escape_javascript(render(partial: 'ro_production_import_model', locals: {message: file_upload_fail('message', @job.job_id), job_id: file_upload_fail('error_code', @job.job_id), error_logs: file_upload_fail('error_log', @job.job_id)} )) %>");
          $('#message-model').show();
          $('.close-model').click(function(){
           $('#message-model').hide();     
          })
          $('#file').val('');
          $('.error_link').click(function(event){
           $('.error_block').removeClass("error_messages_block");
           event.preventDefault();
          });
          clearInterval(checkStatus)
        }
      },
      error: function(xhr) {
        console.log("An error occured: " + xhr.status + " " + xhr.statusText)
      }
    })
  },5000)
<% end %>

下面的行尽管条件为false,但在页面加载时立即调用,而在条件为true时不调用。

$(".model-view").html("<%= escape_javascript(render(partial: 'ro_production_import_model', locals: {message: file_upload_fail('message', @job.job_id), job_id: file_upload_fail('error_code', @job.job_id), error_logs: file_upload_fail('error_log', @job.job_id)} )) %>"); 

为什么会这样?

请帮助。

0 个答案:

没有答案