Ruby-on-rails 使用 Paperclip 获取服务器文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4984604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:24:52  来源:igfitidea点击:

Get server file path with Paperclip

ruby-on-railsrubyfile-uploadpaperclip

提问by seadowg

I'm using Rails with Paperclip to make a small file upload app. I would like to be able to return the file path on the server of the uploaded file once its done but I can't seem to work out how to get the path? Paperclip only seems to record the name of the file itself.

我正在使用 Rails 和 Paperclip 来制作一个小文件上传应用程序。我希望能够在上传文件的服务器上返回文件路径,但我似乎无法弄清楚如何获取路径?Paperclip 似乎只记录了文件本身的名称。

Does anybody now how to do this?

现在有人如何做到这一点?

回答by idlefingers

Assuming you had an attachment called avatar on an instance of a user, you can use user.avatar.pathto get the full path of the file on the filesystem, and you can use user.avatar.urlto give the path which you could use in image tags and whatnot.

假设您在用户实例上有一个名为 avatar 的附件,您可以使用它user.avatar.path来获取文件系统上文件的完整路径,并且可以使用它user.avatar.url来提供可以在图像标签等中使用的路径。

Is that what you're meaning?

这就是你的意思吗?

回答by willawill

I came cross the same problem, so I made a link to it's url in show.html.erb. It works.

我遇到了同样的问题,所以我在 show.html.erb 中做了一个指向它的 url 的链接。有用。

<p>
<b>Pdf:</b><%= link_to "PDF" , @product.pdf.url %>
</p>