Paperclip
allows you to upload files at your preferred location. You can modify its options such as :url
, :path
, :default_url
as you desire.
Before we move further, let me give you little bit idea of what these options are for:
:url - The full URL of where the attachment is publicly accessible.
:path - The files that are assigned as attachments are, by default, placed in the directory specified by this option.
:default_url - The URL that will be returned if there is no attachment assigned.
:styles - A hash of thumbnail styles with geometries. If you need copies of uploaded files with particular dimensions then specify them here.
Lets take step by step approach here:
Your first requirement as below:
I need to change my :url, :path, :default_url so I can reach my example-data.csv file in the controllers directory.
Yes, its possible. Your current configuration as shown below would just work fine without any changes.
has_attached_file :csvdata,
:url => "/controllers/original/:style/:basename.:extension",
:path => ":rails_root/controllers/original/:style/:basename.:extension",
:default_url => "/controllers/original/example_data.csv"
BUT there are some gotchas here which I would like to highlight. With the above setup, whenever you upload a file, it will be stored under application_folder/controllers/original/original directory. The last part of path /original is due to /:style
specified in :path
option. As you have not specified any :styles
option for the attachment, only default style i.e., original
will be created.
Please note that the controllers folder resides at application root level which is a non-publicly accessible folder.
Only the files uploaded under public folder(and its descendants) are accessible via the web browser. If you don't plan to display the uploaded files anywhere in your view and/or just need it for some background processing, then, your current configuration does not require any change. It will work fine as it is.
If this is what you intended to achieve then no further action required.
BUT if you are planning to display or access the uploaded file within a web application then you would need to place it within public
folder which is accessible via browser. In that case, your code would need some changes.
## Update current configuration as below
has_attached_file :csvdata,
:url => "/controllers/:style/:basename.:extension",
:path => ":rails_root/public/controllers/:style/:basename.:extension",
:default_url => "/controllers/:style/example_data.csv"
NOTE: I removed /original
from the path as :style
would create original
folder.
Otherwise, folders would be /controllers/original/original/..
With the above setup, whenever you upload a file, it will be stored under application_folder/public/controllers/original directory. And you can easily access the uploaded file with a simple call like @model_instance.csvdata.url
within your view. For example, as per your question you are uploading a file named example_data.csv
then in your view you can use it as:
## Replace @model_instance with Model instance (contains `csvdata` attachment)
<%= link_to "Example Data", @model_instance.csvdata.url %>
which will generate a clickable link to the uploaded file as:
<a href="/controllers/original/example_data.csv?12345678">Example Data</a>
NOTE: ?12345678
is a random seed appended by rails.
About :default_url
:default_url => "/controllers/:style/example_data.csv"
Its always a good practice to specify :default_url
option in the configuration. The reason for that is in case user doesn't upload a file while creating a record but later tries to access the uploaded file
for this record then they would be directed to the link set in default_url
. Also, note that for this particular record all the attachment
related columns would be set to nil
.
Also, I am confused why would you want the default_url point to example_data.csv
when you want to upload the same file in the application. The standard way is to have a default file named as missing.csv
or missing.png
which is not associated to your uploaded files. Please remember to place missing.csv
or missing.png
inside the /controllers/original/
folder i.e., :default_url
path. Otherwise, upon clicking link you would get an error.