且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Ruby on Rails 中链接到外部文件

更新时间:2023-10-22 23:18:16

您应该将文件放在应用程序的 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.