Rails - 如何在ActiveJob中使用ActionDispatch :: Integration :: Session

时间:2017-11-08 18:05:06

标签: ruby-on-rails rails-activejob actiondispatch

在Rails的控制台app中定义为

[1] pry(main)> app
=> #<ActionDispatch::Integration::Session:0x000000189028e8

现在,我有一个简单的工作:

class MyJob < ActiveJob::Base
  queue_as :low

  def perform
    app.get('/my/path', nil, {'Accept-Language' => "it"})
  end
end

如果我致电MyJob.perform_now

  

NameError:未定义的局部变量或方法`app'for

     

如何在Rails的ActiveJob中使用app

1 个答案:

答案 0 :(得分:1)

class MyJob < ActiveJob::Base
  queue_as :low

  def perform
    app = ActionDispatch::Integration::Session.new(Rails.application)
    app.get('/my/path', nil, {'Accept-Language' => "it"})
  end
end