如何使用Rails从png链接到pdf? [英] How to link to a pdf from a png with Rails?

查看:75
本文介绍了如何使用Rails从png链接到pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面的一部分上有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆