Recently, I came across a helpful gem called this gem, which allows for the seamless integration of bootstrap progress bars and the delayed job gem. The example provided by the creator uses .haml files, but since my project utilizes erb and coffeescript, I attempted to replicate his approach.
Here is a snippet from my controller:
def export
@job = Delayed::Job.enqueue StandingsJob.new
end
This is what I have in the routes.rb file:
get 'export', to: 'scraper#export'
And this is how my home.erb.html looks like:
<%= 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>
Additionally, here's a snippet from 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)
While the code functions correctly upon clicking the link, I'm facing an issue figuring out how to pass the ID of the job created in the controller action to the coffeescript file. Any suggestions would be greatly appreciated!
[EDIT]
This is what I have in the job class:
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
Upon further investigation, it appears that the perform method of my job is not being triggered (I added a puts 'lol' statement but never saw the output in the console).