(圖像屬於郵政)Rails的:通過Carrierwave上傳圖片到Amazon S3僅適用於「編輯」
圖像上傳作品,但只有當我上傳的帖子編輯圖像。我想要在創建帖子時上傳圖片。
圖片上傳(「<%= f.file_field:image%>」)位於新帖子表單中,所以我猜測Carrierwave正在嘗試在創建帖子之前將圖片上傳到指定路徑,導致它不知道去哪裏上傳。
這裏是ImageUploader文件:
class TrackImageUploader < CarrierWave::Uploader::Base
def cache_dir
"#{Rails.root}/tmp/uploads"
end
include CarrierWave::RMagick
#storage :file
#storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :featured do
process :resize_to_fill => [210, 145]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
我猜測從上面的代碼的store_dir負責在亞馬遜S3中根據路徑保存圖像的。
我怎樣才能讓它在發佈後上傳圖片,以便它可以獲得Post.id?
你可以發佈模型代碼嗎? – John
@John在這裏你去:https://gist.github.com/2994293謝謝! – kibaekr
嗯,你可以試着在你的模型中使用AR回調來查看是否強制保存有助於任何事情。 http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html – John