18

A question on Active Storage: I just updated to rails 5.2 and I am trying to open the attached document which is saved as a blob. Currently, it's redirecting to root_path, any idea how can I open/download it instead?

My code in Rails view is:

<%= link_to(document.filename, rails_blob_path(document, disposition: "attachment")) %>

Grant Crofton
  • 8,895
  • 5
  • 28
  • 38
Manav
  • 561
  • 3
  • 10
  • 20

3 Answers3

42

To Download:

<%= link_to document.filename, rails_blob_path(document, disposition: 'attachment') %>

To Preview:

<%= link_to document.filename, rails_blob_path(document, disposition: 'preview') %>

Source - ActiveStorage#Linking to Files documentation

ndnenkov
  • 35,425
  • 9
  • 72
  • 104
Horacio
  • 1,794
  • 1
  • 16
  • 23
8

This should work for you.

<%= link_to 'download', rails_blob_path(document, disposition: "attachment") %>
user953533
  • 321
  • 2
  • 6
1

In my case, and if it helps others, I had to blob element in the path, so:

<%= link_to document.file.blob.filename, rails_blob_path(document.file.blob, disposition: 'preview') %>

malditojavi
  • 1,074
  • 2
  • 14
  • 28