1

I found this gem to use the bootstrap progress bar and the delayed job gem together. In his example he uses .haml file, but I'm using erb and coffeescript in my project, so I tried to reproduce what he does.

This is my controller

def export
    @job = Delayed::Job.enqueue StandingsJob.new
end

this is routes.rb

get 'export', to: 'scraper#export'

and this is my home.erb.html

<%= link_to 'export', export_path, {id:'mario', remote: true} %>

<div class="progress">
  <div class="progress-bar progress-bar-striped active" role="progressbar"
       aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%">
    0%
  </div>
</div>

and this is my coffeescript file

$(document).on "turbolinks:load", ->
    $('#mario').on 'click', ->
      alert('hocliccatoooo')
      interval = setInterval( ->
        $.ajax(
          url: '/progress-job/' **** What should I add here?????****,
          success: (job) ->
            console.log('loool')
            stage
            progress
            if job.last_error != null
              $('.progress-status').addClass('text-danger').text(job.progress_stage);
              $('.progress-bar').addClass('progress-bar-danger');
              $('.progress').removeClass('active');
              clearInterval(interval);

            if job.progress_stage != null
                stage = job.progress_stage
                progress = job.progress_current / job.progress_max * 100
            else
              progress = 0
              stage = 'Uploading file?'

            if progress != 0
              $('.progress-bar').css('width', progress + '%').text(progress + '%')
            $('.progress-status').text(stage);
          error: ->
            alert('errore')
            $('.progress').removeClass('active');
            $('.progress-bar').css('width', '100%').text('100%');
            $('.progress-status').text('Finito!!!');
            clearInterval(interval);

        )
      , 100)

The code runs correctly when I click on the link, the only problem is, how can I tell to my coffeescript the id of the job that I created in my controller action? Thank you all

[EDIT]

This is my job

class StandingsJob < ProgressJob::Base

  def perform
    update_stage 'Faccio cose'
    update_progress_max 10
    for i in [0..10]
      sleep(2)
      update_progress
    end
  end
end

I did some digging and seems that the perfom method of my job is never fired(i put a puts 'lol' in my perform method and I never see it in the console)

Leonardo
  • 499
  • 1
  • 7
  • 18

2 Answers2

1

interval = setInterval( -> var paramJob = '<%=@job.to_json%>'; // get @job parameter ->

//use the string "&" + "quot;" instead of SYM

var jobObj = JSON.parse(('{' + paramJob.slice(paramJob.indexOf("SYMid"), paramJob.length)).replace(/SYM/g, '"'));//The parameters are processed

//in the ajax use jobObj.id url: '/progress-job/' + jobObj.id,

hope this can help you!

代希臻
  • 11
  • 3
0

This is a client side vs server side issue.

You'll have to put the job-id in a data-attribute on the html tag during the server side render. It should be in the @job variable based on your controller action.

Once your page is rendered and you're in the coffeescript code (now client side). You can parse this attribute out and utilize it.

engineerDave
  • 3,887
  • 26
  • 28