在视图中显示 delayed_job 个处理程序

displaying delayed_job handler in a view

你能告诉我如何在我的视图中显示 delayed_job 处理程序吗?我的控制器 returns delayed_job 的 JSON 对象。在执行以下代码后,我在视图中显示 JSON 但不显示处理程序。

****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")

您将作业分配给了 @job 变量,但是当您渲染 JSON 时,您使用了 @delayed_job 变量。

试试这个:

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

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

  end
end

此外,您无需在哈希上调用 .to_json,它会为您完成。