我有一个 rake 任务,它将图像存储在ActiveStorage
. 在同一个 rake 任务中,我需要这些图像的附件 URL。
在正常的 Rails 上下文中,我会使用url_for(my_attachment)
. 但是,该助手在 rake 任务中不可用。
我试图包括路线助手:
task my_task: :environment do
include MyApp::Application.routes.url_helpers
attachment = my_model.image.attach(..)
url_for(attachment)
end
结果是:
*** NoMethodError Exception: undefined method `attachment_url' for main:Object
有没有办法在 rake 任务中获取附件的公共 URL?