views:

23

answers:

1

As the title says, I want to build a ruby web page that dynamically links all files in a directory. Does anyone have any sample code or basic suggestions as to how to do it?

Thanks in advance for your help!

A: 

Use the Dir class, either with Dir.entries to list the directory, or with Dir.glob for a bit more flexibility. Keep in mind that entries gives you names only, while glob will include the full relative path.

You could use an action like this:

def index
  root = "#{RAILS_ROOT}/public"
  @files = Dir.entries(root).reject {|x| x.match /^\./}
end

And a view:

<% @files.each do |path| %>
  <li><a href="<%= path %>">
         <%= File.basename path %></a>
<% end %>
jleedev
Very nice, I appreciate the answer, That was the direction I was going, but you saved me a lot of trial and error. Thanks!
nomoreflash