Reading files from dir

Estelle Winterflood wrote:

I have gif files in my public/images/posticons directory, I want to read their filenames without the extension to list the files as a set of radio buttons in the form:

There is also one icon called default.gif that I don't want to list and only use it when there is no other icon chosen. I can do that myself I'm just having troubles getting all the files to show up from this directory.

Here's what I've done below ("myImages" is a subdirectory of the "images/" directory of public) and it works just fine as long as you are currently in the directory that has the files or in the parent of it. I haven't used it in my app from a view, just through the IRB console.

d = Dir.entries("myImages") d.foreach do |f|   filename = f.delete(f.slice(/\..../)) end

That removes the ".ext" from the string and you're left with just the filename.

You could use the File I/O methods to read the attributes of the file but why bother when this works too.

d = Dir.entries("myImages") d.foreach do |f|   filename = f.delete(f.slice(/\..../)) end

Correction see below...

d = Dir.entries("myImages") d.each do |f|   filename = f.delete(f.slice(/\..../)) end