Ruby on rails 如何在resque作业中使用rails助手?
我试图在我的工作中使用一些助手,但遇到了一些问题。以下是我尝试过的:Ruby on rails 如何在resque作业中使用rails助手?,ruby-on-rails,resque,Ruby On Rails,Resque,我试图在我的工作中使用一些助手,但遇到了一些问题。以下是我尝试过的: require 'json' class SoulmateUserFollowing tried this -> include Rails.application.routes.url_helpers and this -> include ActionView::Helpers:UrlHelper and this -> helper ActionView::Helpers::UrlHelp
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文件中添加命名路由,然后从作业类调用它(无需包含任何内容) 您还必须在您的环境中设置默认主机,因为您在“resque”中,并且没有设置http参数
routes.default_url_options = {:host => "somehost.com"}
或者,您可以包括url\u帮助程序,并在类中执行类似操作
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
太好了,真管用。然而,我仍然不明白为什么包括url\u助手,然后调用user\u path(user)或其他什么都不起作用?此外,我还需要弄清楚如何包含用于image_path的rails资产帮助程序和我自己的ImageHelper模块,该模块位于普通rails帮助程序文件夹中。你知道怎么做吗?它应该通过扩展url_helpers或你的ImageHelper来工作,因为你在一个类方法中,而不是一个实例方法,但是其他东西可能也会丢失。
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