在 Ruby on Rails 中链接到外部文件 [英] Linking to external file in Ruby on Rails
问题描述
在 Ruby on Rails 中链接到外部文件
Linking to external file in Ruby on Rails
我有一个文件,我想在我的 rails 应用程序中作为链接共享.我在这样的 .erb 文件中创建了一个链接
I have a file I want to share as a link in my rails app. I create a link to it in the .erb file like this
<li><a href="somefile.pdf">Some File</a> </li>
当我选择链接时,出现以下错误.
When I select the link I get the following error.
路由错误
没有路由匹配/pages/somefile.pdf"使用 {:method=>:get}
No route matches "/pages/somefile.pdf" with {:method=>:get}
我真的需要一条路线吗?我真的只想弹出另存为对话框.只是指向用户可以打开或下载的文件的链接.
Do I really need a route for this? I really just want the save as dialog to popup. Just a link to a file that the user can open or download.
rails 的方法是什么(这里是 rails 新手)?
What is the rails way to do this (rails newbie here)?
推荐答案
您应该将文件放在应用程序的 public/
目录中,并在链接的 href 路径的开头使用正斜杠.
You should place the file in the app's public/
directory and use a forward slash at the start of the path in your link's href.
您遇到的问题是因为 href="somefile.pdf"
与当前 URL 相关,这可能类似于 http://localhost:3000/pages/42代码>.通过使用
href="/somefile.pdf"
而解析的 URL 将是 http://localhost:3000/somefile.pdf
(而不是 http:///localhost:3000/pages/somefile.pdf
) 并且它不会与您的页面路由冲突.
The problem you are having is because href="somefile.pdf"
is relative to the current URL which is probably something like http://localhost:3000/pages/42
. By using href="/somefile.pdf"
instead the resolved URL will be http://localhost:3000/somefile.pdf
(rather than http://localhost:3000/pages/somefile.pdf
) and it won't conflict with your pages routes.
这篇关于在 Ruby on Rails 中链接到外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!