Search code examples
ruby-on-railsajaxjsonhandlerdelayed-job

displaying delayed_job handler in a view


Can you tell me how to display a delayed_job handler in my view? my controller returns a JSON object of delayed_job. upon the below code execution I am displaying JSON in my view but not the handler.

****delayed_job_controller.rb****
def show
  @job = Delayed::Job.find(params[:id])

  respond_to do |format|
    format.json { render(json: {payload: @delayed_job.handler}.to_json) } 
    format.html

  end
end

****_list_row.html.haml****

%td=link_to item.id, admin_delayed_job_path(item)
%td=link_to "[+]", '#', class: 'handler'
- %w(priority attempts created_at run_at failed_at).each do |k|
  %td= item.send k

-if can? :destroy, item
  %td=link_to "Delete",admin_delayed_job_path(item), method: :delete

****delayed_jobs.js.coffee****

class DelayedJobAdmin
  constructor: (tableSelector) ->
  @table = $(tableSelector)
  console.log @table
  @initBehaviours()

initBehaviours: =>
  @table.find('a.handler').click (e)=>
    e.preventDefault()

    p = $.ajax {
      url: '/admin/delayed_jobs/280.json'
      success: (res) ->
        $.each res, (index, element) ->
        $('.handler').html(element)



      error: (res) ->
        console.log 'error',res
    }


 $(document).on 'ready', ->
  new DelayedJobAdmin(".delayed-job-admin")

Solution

  • You assigned the job to the @job variable, but when you render the JSON you use the @delayed_job variable.

    Try this:

    def show
      @job = Delayed::Job.find(params[:id])
    
      respond_to do |format|
        format.json { render(json: {payload: @job.handler}) } 
        format.html
    
      end
    end
    

    Also, you don't need to call .to_json on the hash, it will be done for you.