How to link to a pdf from a png with Rails?

我与影子孤独终老i 提交于 2019-12-24 12:28:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!