我正在尝试在我的 resque 工作中使用一些 helper ,但遇到了问题。这是我尝试过的:
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)中执行类似的操作
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
关于ruby-on-rails - 如何在 resque 工作中使用 Rails 助手?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7732421/