Reputation: 510
I want to understand how to download file using Paperclip. I upload file to local storage.
It's Model:
class AFile < ActiveRecord::Base
has_attached_file :attach,
:url => "public/attach/:basename.:extension",
:path => ":rails_root/public/attach/:basename.:extension"
validates_attachment_content_type :attach, content_type: "text/plain"
end
It's View show.html.erb :
<p>
<strong>AFile:</strong>
<%= @afile.name_file %>
</p>
<%= link_to 'Download', @afile.attach.url(:original, false) %> |
<%= link_to 'Edit', edit_afile_path(@afile) %> |
<%= link_to 'Back', afiles_path %>
I did like this: File download using Paperclip but it did not help.
But when i click on the Download, then an error: No route matches [GET] "/public/attach/text.txt"
How to solve this problem? Why file cannot be downloaded by clicking "Download"?
Upvotes: 0
Views: 1786
Reputation: 1
My solution to download file is something like :
<%= link_to 'Download', @afile.attach.url(:original),
download: @afile.attach.url(:original)%>
Upvotes: -1
Reputation: 101811
Rails places the /public
directory in the servers web root. So a file with the file system path /public/foo.txt
will be accessible at http://localhost:3000/foo.txt
- not http://localhost:3000/public/foo.txt
.
So you need to change url
option for the attached file:
class AFile < ActiveRecord::Base
has_attached_file :attach,
:url => "/attach/:basename.:extension",
:path => ":rails_root/public/attach/:basename.:extension"
validates_attachment_content_type :attach, content_type: "text/plain"
end
Upvotes: 2