如何使用Rails从png链接到pdf? [英] How to link to a pdf from a png with Rails?
问题描述
我在页面的一部分上有一个map.png文件的图像.我希望此图像是可单击的,然后下载该图像的pdf版本.我一直以此为参考 Rails 3.1,可以吗?不能将link_to设置为image_path?,但是我不确定如何继续.
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!
推荐答案
您是否有通往pdf下载的路径,或者文件本身是静态资产?
Do you have a route to the pdf download or is the file itself a static asset?
您可以将标准link_to
帮助器与image_tag
帮助器一起使用,以创建可点击的图像.
You can use a standard link_to
helper with an image_tag
helper to create a clickable image.
对于静态pdf资产:
<%= link_to(image_tag('my_image.png'), 'path/to/filename.pdf') %>
这将在页面上显示图像my_image.png
,单击该图像将开始下载或显示静态pdf资产.
This will show the image my_image.png
on the page which when clicked will begin downloading or displaying the static pdf asset.
对于投放文件的控制器操作:
页面:
<%= link_to(image_tag('my_image.png'), download_pdf_path) %>
控制器:
def download_pdf
send_file 'path/to/filename.pdf'
end
路线:
get 'download_pdf' => 'controller#download_pdf'
这将在页面上显示图像my_image.png
,单击该图像将请求获取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.
这篇关于如何使用Rails从png链接到pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!