ruby-on-rails – 如何在resque作业中使用rails helpers?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何在resque作业中使用rails helpers?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在我的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
原文链接:https://www.f2er.com/ruby/264495.html

猜你在找的Ruby相关文章