You don't even need the download
controller action, you can just generate a download-friendly link like so:
attachment.rb
def download_url
S3 = AWS::S3.new.buckets[ 'bucket_name' ] # This can be done elsewhere as well,
# e.g config/environments/development.rb
url_options = {
expires_in: 60.minutes,
use_ssl: true,
response_content_disposition: "attachment; filename=\"#{file_name}\""
}
S3.objects[ self.path ].url_for( :read, url_options ).to_s
end
<%= link_to 'Download Avicii by Avicii', attachment.download_url %>
If you still wanted to keep your download
action for some reason then just use this:
attachments_controller.rb
def download
redirect_to @attachment.download_url
end
Thanks to guilleva for his guidance.
You can manage your file downloading with separate controller, if you don't want to eal with HTTP server configurations.
So you can send_file with disposition
option as attachment
.
Depends on how you / where you serve the file itself. I do not have experience with ruby but if you can alter the headers(most platforms offer this option) of the http response you can force a download. This requires:
Content-Type: application/force-download
I guess it will use "Content-type: application/octet-stream" by default which will cause the browser to play it.
But this will only work if you have control over the server/location that holds the actual file since you need to change the response when the file is sent to the browser.