Web page that links all files in a directory
I want to build a ruby web page that dynamically links all f开发者_JS百科iles in a directory. Does anyone have any sample code or basic suggestions as to how to do it?
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 %>
精彩评论