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

Posted . Visible to the public.

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

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
Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2013-12-16 17:31)