Rails 3.1 Paperclip - better way to organize folder structure for uploaded files?
-
26-10-2019 - |
Question
I am using Paperclip to save files for Rails 3.1 app. Everything works fine, although I am a bit confused by the fact that everything is saved to the Public folder..
I have this kind of structure:
class Photo < ActiveRecord::Base
belongs_to :photoable, :polymorphic => true
has_attached_file :photo, :styles => { :large => "800x600", :medium => "400x300>", :thumb => "100x100>" }
end
class Project < ActiveRecord::Base
attr_accessible :projectname, :photos_attributes
has_many :photos, :as => :photoable
accepts_nested_attributes_for :photos, :allow_destroy => true
end
Obviously, there can be another models which also has_many :photos, :as => :photoable
Then if I upload a photo, it is saved to Public/system/photos/1 (or /2 etc.). Everything works fine, but I wonder if there is a better way to organize it (so that the folder structure is a bit more human-readable (has usernames, project names etc.)?
Also correct me if I am wrong, but I thought that the files should go to assets and not Public folder?
Thanks
Solution
Content that is uploaded by users should go into public/system folder, while design elements like stylesheets, images and javascripts should be placed in app/assets corresponding folder.
If you want to add to file path some additional information that is not supported by default (like username), then you should take a look at paperclip interpolations ( https://github.com/thoughtbot/paperclip/wiki/Interpolations )
OTHER TIPS
This is what the :path option is for in has_atached_file. Check out the "Storage" section of the README: https://github.com/thoughtbot/paperclip
The files that are assigned as attachments are, by default, placed in the directory specified by the :path option to has_attached_file. By default, this location is :rails_root/public/system/:attachment/:id/:style/:filename. This location was chosen because on standard Capistrano deployments, the public/system directory is symlinked to the app's shared directory, meaning it will survive between deployments. For example, using that :path, you may have a file at