###前提・実現したいこと
CarrierWaveで画像アップロード機能を作っているのですが、仕様でfilenameを#{model.id}.jpgのようにできないことで詰まっています。
参考↓ (app/uploaders/image_uploader.rb)
class ImageUploader < CarrierWave::Uploader::Base ###省略 # Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for details. def filename "#{model.id}.#{file.extension}" if original_filename end end
ちゃんとAvoid using model.idと書かれています。。。。
ファイル名を決定するときはまだDBにsaveされていなくてidが決定してないからですね。。
ちなみにgithubのcarrierwaveuploader/carrierwaveの
carrierwave/lib/carrierwave/uploader/store.rbには
## # Override this in your Uploader to change the filename. # # Be careful using record ids as filenames. If the filename is stored in the database # the record id will be nil when the filename is set. Don't use record ids unless you # understand this limitation. # # Do not use the version_name in the filename, as it will prevent versions from being # loaded correctly. # # === Returns # # [String] a filename # def filename @filename end
とありますね。。。これどうしようもないやつなのかな。。
色々関連ページ↓
その1
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。