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

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

when to work they need to be

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

-------------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', '')

or

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

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

Related post

Copyright (C) dskims.com, All Rights Reserved.

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