繁体   English   中英

如何在resque作业中使用rails helpers?

[英]how do I use rails helpers in resque jobs?

我正在尝试在我的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中包含我的自定义帮助程序。

在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM