Permalink
Browse files

handle attahcment with send_file

  • Loading branch information...
dzaporozhets committed Feb 11, 2013
1 parent ab0cfc0 commit a699ebdbcc11051b9473a88788cf8efdde659975
@@ -0,0 +1,8 @@
+class FilesController < ApplicationController
+ def download
+ uploader = Note.find(params[:id]).attachment
+ uploader.retrieve_from_store!(params[:filename])
+ send_file uploader.file.path, disposition: 'attachment'
+ end
+end
+
@@ -19,4 +19,8 @@ def image?
rescue
false
end
+
+ def secure_url
+ "/files/#{model.class.to_s.underscore}/#{model.id}/#{file.filename}"
+ end
end
@@ -26,7 +26,7 @@
= markdown truncate(event.target.note, length: 70)
- note = event.target
- if note.attachment.url
- = link_to note.attachment.url, target: "_blank", class: 'note-file-attach' do
+ = link_to note.attachment.secure_url, target: "_blank", class: 'note-file-attach' do
- if note.attachment.image?
= image_tag note.attachment.url, class: 'note-image-attach'
- else
@@ -31,7 +31,7 @@
- if note.attachment.image?
= image_tag note.attachment.url, class: 'note-image-attach'
@andyjeffries

andyjeffries Jun 18, 2013

Why do you use .url here rather than .secure_url? On my server inline images are broken, but the download links work correctly. I can't see how this should be different - if you use secure_url to download the attachment and the attachment is an image, why not use secure_url as the source?

.attachment.pull-right
- = link_to note.attachment.url, target: "_blank" do
+ = link_to note.attachment.secure_url, target: "_blank" do
%i.icon-paper-clip
= note.attachment_identifier
.clear
View
@@ -45,6 +45,11 @@
root to: "projects#index"
end
+ #
+ # Attachments serving
+ #
+ get 'files/:type/:id/:filename' => 'files#download', constraints: { id: /\d+/, type: /[a-z]+/, filename: /[a-zA-Z.0-9_\-\+]+/ }
+
#
# Admin Area
#

0 comments on commit a699ebd

Please sign in to comment.