Read more

CarrierWave: Don't use #url when you mean a file path

Henning Koch
December 16, 2013Software engineer at makandra GmbH

CarrierWave attachments have two distinct methods #url and #path which appear to behave the same:

document.file.url   # => /storage/documents/4/letter.doc
document.file.path  # => /storage/documents/4/letter.doc
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

However, starting with CarrierWave 0.9, #url will properly escape high-ASCII characters:

document.file.url   # => /storage/documents/4/h%C3%BCte.doc
document.file.path  # => /storage/documents/4/hüte.doc

So always use #url if you mean an actual URL (e.g. to display an <img>). But use #path if you want the local file path (e.g. for #send_file).

Note that other than #url, #file does not take an argument for the requested version. Request a version like this instead:

document.file.preview.url
document.file.preview.path
Posted by Henning Koch to makandra dev (2013-12-16 18:31)