1

I have an image of a map.png file on part of a page. I would like this image to be clickable, and then to download the pdf version of that image. I've been using this as a reference Rails 3.1, can't make link_to image_path?, but I'm not sure how to proceed.

It looks like I also need to edit something with the way the page is routed. Thanks for all the help!

Community
  • 1
  • 1
vpoola88
  • 3,669
  • 4
  • 20
  • 21

1 Answers1

1

Do you have a route to the pdf download or is the file itself a static asset?

You can use a standard link_to helper with an image_tag helper to create a clickable image.

For a static pdf asset:

<%= link_to(image_tag('my_image.png'), 'path/to/filename.pdf') %>

This will show the image my_image.png on the page which when clicked will begin downloading or displaying the static pdf asset.

For a controller action that serves the file:

Page:

<%= link_to(image_tag('my_image.png'), download_pdf_path) %>

Controller:

def download_pdf
  send_file 'path/to/filename.pdf'
end

Route:

get 'download_pdf' => 'controller#download_pdf'

This will show the image my_image.png on the page which when clicked will make a get request to the pdf download action.

Matt
  • 13,948
  • 6
  • 44
  • 68