2

在 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”

如何app在 Rails 的 ActiveJob 中使用?

4

1 回答 1

5
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
于 2017-11-08T18:20:34.423 回答