Timus83
Timus83

Reputation: 681

Paperclip. Handling files with identical filenames

If I save two files with similar filenames which will go into the same directory, one will overwrite another. How to make to work the following way, let's say, there is already picture.jpg in a directory, and if I paperclip uploads another picture.jpg into the same directory, it will automatically rename it to picture-1.jpg and so on. Thanks in advance.

Upvotes: 1

Views: 191

Answers (2)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

Configure the attachment to include the primary ID of the record to ensure there can't be duplicates... something like this:

:path => ':rails_root/public/system/assets/photos/:id/:style.:extension',
:url =>                    '/system/assets/photos/:id/:style.:extension'

To be clear... the above also removes the original filename, but there's an expansion keyword to get that back in as well... I just never use it.

Upvotes: 2

Antarr Byrd
Antarr Byrd

Reputation: 26061

You can randomize the filenames if you like.

Upvotes: 2

Related Questions