在一個應用程序中,我想將公共文件URL發送到after_create
回調中的服務。所以,代碼(簡化)看上去像這樣:Carrierwave在回調中返回tmp文件的路徑而不是實際的
class UserProfile < ApplicationRecord
mount_uploader :video, VideoUploader
after_create :send_url_to_service
private
# Just logs the URL
def send_url_to_service
Rails.logger.info video.url
end
end
令我沮喪,上傳之後,send_url_to_service
回調始終記錄緩存的文件路徑 - 像'uploads/tmp/1473900000-123-0001-0123/file.mp4'
而不是'uploads/user_profiles/video/1/file.mp4'
。我試圖編寫一個方法來從實際的文件路徑中構建URL,但它不起作用,因爲該文件還沒有。
所以,問題是,你如何在這樣的情況下獲得最終文件的URL?
P. S.請注意,這是一個自我回答的問題,我只是想分享我的經驗。