【问题标题】:Rails - how to use ActionDispatch::Integration::Session in ActiveJobRails - 如何在 ActiveJob 中使用 ActionDispatch::Integration::Session
【发布时间】:2017-11-08 18:05:05
【问题描述】:

在 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: undefined local variable or method `app' for

如何在 Rails 的 ActiveJob 中使用 app

【问题讨论】:

  • 这样的工作的目标是什么?看起来很奇怪……我考虑到了一种可能性,就是你走错了方向
  • 您只是想获得页面的渲染响应吗?将其保存到静态文件或什么的?如果您在 rails 5 上,则比使用 app.get 更简单
  • 我需要调用一个控制器的方法,主要是为了触发一些缓存相关的东西

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 2016-03-25
    • 2011-11-25
    • 2016-10-22
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    相关资源
    最近更新 更多