问题
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!
回答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.
来源:https://stackoverflow.com/questions/16878267/how-to-link-to-a-pdf-from-a-png-with-rails