P
P
Perzh2014-04-13 17:30:00
Ruby on Rails
Perzh, 2014-04-13 17:30:00

How to properly organize the storage of user files on the server?

Hello.
I use paperclip to upload files to the server. At the same time, I expect that there will be a lot of files, so I want to somehow organize the ways to save files. I tried doing like this:

has_attached_file :file, url: '/downloads/:id/:basename.::extension'

and it works, but I would like to separate the folders more into categories (files upload belongs_to category), but
has_attached_file :file, url: '/downloads/:category_id/:id/:basename.::extension'

no longer works, swears at category_id - an unknown parameter. I would also like to start my own folder for each date, but I don’t even know how to do this. In this regard, the question is: how to edit the paths where the files will be saved? Or, if it's not difficult, explain how files should be stored, maybe I'm too bother with this? Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arkady Butermanov, 2014-04-13
@Perzh

1. In initializer add:

Paperclip.interpolates :category_id do |attachment, style|
    attachment.instance.category_id.to_s
end

Then your path will work.
2. I don’t know about others, but we prefer to store files
a) on AWS S3
b) use standard paths (in Paperclip this is :rails_root/public/system/:class/:attachment/:id_partition/:style/:filename ) , because they are not just called standard.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question