在视图中显示delayed_job处理程序

时间:2015-03-30 09:49:29

标签: ruby-on-rails ajax json handler delayed-job

你能告诉我如何在我的视图中显示delayed_job处理程序吗?我的控制器返回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")

1 个答案:

答案 0 :(得分:1)

您已将作业分配给@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,它将为您完成。