Listing/linking to contents of directory in Rails

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

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

Current code:

<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>

However the links are still coming as


when to work they need to be


-------------Problems Reply------------

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

The chomp method is used to remove someting at the end of the string ;) Use gsub instead.

new_file.gsub!('public', '')


new_file = new_file.gsub('public', '')

Category:ruby on rails Views:0 Time:2011-04-27

Related post

Copyright (C), All Rights Reserved.

processed in 0.150 (s). 11 q(s)