Listing / link to directory contents in Rails

I have a set of static PDF files. I want to list them on the rails page with links to them.

I need right now how to trim / public from the beginning of the path for the link to 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 still go like

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

when you need to work, they should be

 http://localhost:3000/files/document.pdf 
+8
file ruby-on-rails
source share
2 answers
 <% Dir["public/files/*.pdf"].each do |file| %> <li><%= link_to 'Letter', file[/\/.*/] %></li> <% end %> 
+6
source share

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

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

or

 new_file = new_file.gsub('public', '') 
+3
source share

All Articles