アプリなどを開発するブログ

React Native / Swift / Ruby on Railsなどの学習メモ。


Paperclipで日本語ファイル名を避けるためにシステム側でリネーム

Paperclipで日本語ファイル名を使うとエラーが出ることがあります。
なのでもはや元ファイル名を使わず、style名 + 拡張子名とすることにしました。

initializers / paperclip.rb

case Rails.env
when 'production'
  Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
  Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style.:extension'
  Paperclip.options[:log] = true
  Paperclip.options[:log_command] = true
else
  Paperclip::Attachment.default_options[:path] = ":rails_root/public/system/:class/:attachment/:id_partition/:style.:extension"
  Paperclip::Attachment.default_options[:url] = 'http://localhost:3000/system/:class/:attachment/:id_partition/:style.:extension'
end

これで、Paperclipが勝手にoriginal.png small.png みたいにリネームしてくれます。