3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Paperclipのファイル格納フォルダ名に日付をつけました

Last updated at Posted at 2013-12-19

Paperclip( https://github.com/thoughtbot/paperclip ) を利用しファイルのアップロードをやってます。

ファイルの格納先フォルダにクラス名とかIDとかが利用できますが、やっぱり日付をつければ分かりやすく、なしにもファイルの管理ができます。

has_attached_file`に使えるinterpolationはhttps://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/interpolations.rb に詳しく乗っていますので、ご参考ください。

まずは日付を含むInterpolationsを作りましょう。

# file path: RAILS_ROOT/config/paperclip.rb(ここじゃくてもよいです。)
Paperclip.interpolates :ymd_path do |attachment, style|
  attachment.instance.avatar_updated_at.utc.strftime "%Y/%m/%d"
end

これで新たに:ymd_pathを定義しました。

次はModelの中に:

has_attached_file :data,
                    :url  => "/pictures/:ymd_path/:id/:style_:basename.:extension",
                    :path => "pictures/:ymd_path/:id/:style_:basename.:extension",

これでpictures/2013/12/19/7/content_recent-happening.pngのようなファイルが作られます。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?