Forum: Ruby on Rails Reading files from dir

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nathan V. (Guest)
on 2006-02-23 01:52
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:

[code]<%= radio_button("thread", "posticon", imgname) %>
<img src="/images/posticons/<%= imgname%>.gif" alt="" />[code]

Would I be saving myself a lot of time and server resources by just
saving all of the posticons into the database instead? It's pretty
convenient being able to just put and change the post icons by adding
and removing files from this directory.

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.
Estelle W. (Guest)
on 2006-02-26 08:18
(Received via mailing list)
> 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.

Nathan,

Did you have any luck with this?

Did you end up using the Dir and/or File classes (in the core Ruby
library)?

I need to do something similar but can't find any code examples.

Regards,
Estelle.
Corey M. (Guest)
on 2008-03-13 22:11
Estelle W. 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.
Corey M. (Guest)
on 2008-03-13 22:12
> 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
This topic is locked and can not be replied to.