ruby-on-railsdragonfly-gem

How to create a link to a non-image file (like a word or excel file) using Dragonfly and Rails?


I've got a rails app with the ability to upload multiple types of files and now i'm trying to create a page to show the data to the user.

I have this snippet in my view:

<% @case.documents.each do |doc| %>
  <% if doc.is_image? %>
    <%= image_tag doc.document.url  %>
  <% else %>
    <%= link_to doc.filename, doc.document.url, target: '_blank' %>
  <% end %>
<% end %>

which generates a link like this for a file that is not an image.

<a target="_blank" href="/media/W1siZiIsIjIwMTQvMDcvMjgvOHM3d21ubDBtc19maWxlIl1d?sha=0813902f">myFile.xlsx</a>

The problem is that since the href doesn't link directly to a filename with an explicit extension, the browser is not sure how to handle the file (at least I think that's why it doesn't know how to handle the file).

How can I make a link to these files which have been uploaded so the browser knows how to handle files like excel, word, pdf, etc.


Solution

  • the easiest way is to add a document_name column to your Document model - then the urls will have the file extension.

    (although having said that, the Content-Type should be correctly set anyway so not sure why the browser is having trouble)