列出/链接到Rails中的目录内容 [英] Listing/linking to contents of directory in Rails

查看:60
本文介绍了列出/链接到Rails中的目录内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组静态PDF.我想将它们列出在rails页面上,并带有指向它们的链接.

I have a set of static PDFs. I want to list them out on a rails page, with links to them.

我现在需要的是如何从路径的开始处修剪/public,以便链接真正起作用.

What I need right now is how to trim the /public off the beginning of the path so the link will actually work.

当前代码:

<h1>Listing letters</h1>
<table>
<ul>
<% @files = Dir.glob("public/files/*.pdf") %>
<% for file in @files %>
<% new_file = file.to_s %>
<% new_file = new_file.chomp("public/") %>
<li><%= link_to 'Letter', new_file %></li>
<% end %>
</ul>
</table>

但是链接仍然以

http://localhost:3000/public/files/document.pdf

工作时必须保持

http://localhost:3000/files/document.pdf

推荐答案

<% Dir["public/files/*.pdf"].each do |file| %>
  <li><%= link_to 'Letter', file[/\/.*/] %></li>
<% end %>

这篇关于列出/链接到Rails中的目录内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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