如何在 resque 作业中使用 Rails 助手?

发布于 2024-12-09 05:13:28 字数 486 浏览 0 评论 0原文

我正在尝试在我的 resque 工作中使用一些助手,但遇到了问题。这是我尝试过的:

require 'json'

class SoulmateUserFollowing
  tried this -> include Rails.application.routes.url_helpers
  and this -> include ActionView::Helpers:UrlHelper
  and this -> helper ActionView::Helpers::UrlHelper

  @queue = :soulmate_user

  def self.perform(user_id)
    user = User.find(user_id)
    url = url_for(following_user)
  end
end

我还需要包含带有 image_path 方法的帮助程序和位于模块 ImageHelper 中的自定义帮助程序。

I'm trying to use some helpers in my resque job and am running into problems. Here's what I've tried:

require 'json'

class SoulmateUserFollowing
  tried this -> include Rails.application.routes.url_helpers
  and this -> include ActionView::Helpers:UrlHelper
  and this -> helper ActionView::Helpers::UrlHelper

  @queue = :soulmate_user

  def self.perform(user_id)
    user = User.find(user_id)
    url = url_for(following_user)
  end
end

I also need to include the helper with the image_path method and a custom helper of mine located in module ImageHelper.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路黄昏 2024-12-16 05:13:28

在 config/routes.rb 文件中添加命名路由,然后从作业类中调用它(无需包含任何内容)

Rails.application.routes.url_helpers.following_user_url(following_user)

您还必须在环境中设置默认主机,因为您位于“resque”内并且没有 http参数设置。

routes.default_url_options = {:host => "somehost.com"}

或者,您可以包含 url_helpers 并在您的班级中执行类似的操作

class SoulmateUserFollowing
  include Rails.application.routes.url_helpers

  @queue = :soulmate_user

  def initialize(user_id)
    user = User.find(user_id)
    url = url_for(following_user)
  end

  def self.perform(user_id)
    new(user_id)
  end
end

Add a named route in your config/routes.rb file and then call it from your job class (no need to include anything)

Rails.application.routes.url_helpers.following_user_url(following_user)

You also have to set in your environment the default host since you are inside 'resque' and there are no http parameters set.

routes.default_url_options = {:host => "somehost.com"}

Alternatively you can include the url_helpers and do something like this in your class

class SoulmateUserFollowing
  include Rails.application.routes.url_helpers

  @queue = :soulmate_user

  def initialize(user_id)
    user = User.find(user_id)
    url = url_for(following_user)
  end

  def self.perform(user_id)
    new(user_id)
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文