Uploading a file to a directory outside of RAILS ROOT with attachment_fu
-
21-09-2019 - |
Question
I'm trying to use attachment_fu to upload files to a directory outside of the RAILS_ROOT
. I want the files to be saved to ~/APP_NAME/uploads/
so that they can be approved/rejected before becoming publicly available. I've tried the following configuration for has_attachment
:
has_attachment :storage => :file_system,
:path_prefix => "~/APP_NAME/uploads/",
:max_size => 5.megabytes
Unfortunately, this configuration simply creates the ~/APP_NAME/uploads/
directory structure in RAILS_ROOT
. Any way to save the file outside of RAILS_ROOT
?
Solution 2
I found an alternative method that suits me better than using relative pathnames. I added a method called full_filename
to my attachment class:
class attachment < ActiveRecord::Base
def full_filename
return "/Users/ron/attachments/#{id}.#{file_format}"
end
end
OTHER TIPS
This probably isn't an Attachment-Fu issue, but rather how Ruby handles File I/O as well as how files are stored in Unix.
So for instance if your app lives in, say, ~/Users/ron/APP_NAME
If you change the above code:
:path_prefix => "~/APP_NAME/uploads/"
To:
:path_prefix => "../#{RAILS_ROOT}/uploads"
The files would be stored in a folder called "uploads" in ~/Users/ron/uploads. The "../" means one directory above the current Rails root. If you want to go up two directories, it would be "../../" and so on.
But that only addresses hierarchical navigation. If you wanted to tell Attachment-Fu to store files in a hardcoded directory in your filesystem, you could give it a file path such as "~/Users/ron/APP_NAME/uploads", but keep in mind hardcoding in a file path this way is brittle and could be a pain point in the future should your file storage requirements change.
Hope that helps.