Welcome Guest | Login

Trying to iterate over a directory

I am trying to display all the images in a particular directory within an .rhtml file. (My website has a Rails app that is integrated with my website, and so I am using the images directory in my website, not the standard 'images' directory in Rails.) I believe that I have a handle to the correct directory, which for my test is ~/railscats/public/images/fosters/2007/Samantha. But when I try to iterate over that directory, the code does nothing. It seems like it isn't seeing any files in that directory. Here is what my directory has in it:
./   Samantha1.jpg  Samantha3.jpg          SamanthaAndEmily2.jpg  Z-ORIGINALS/
../  Samantha2.jpg  SamanthaAndEmily1.jpg  SamanthaEmilyMax.jpg

Here is the code from my .rhtml:
    <%= public_dir = File.expand_path("public", RAILS_ROOT) %><br>
     <%= fullpath = public_dir + "/images/fosters/2007/Samantha" %><br>
     <%= images_dir = Dir.new(fullpath) %><br>
     Printing out list of files:<br>
     <%= images_dir.each { |file|  "hello" } %>

And here is the output:

/home/rodneya/railscats/public
/home/rodneya/railscats/public/images/fosters/2007/Samantha
#
Printing out list of files:
#

Does anyone know what I am doing wrong?

Thanks,
Tina


2007-09-30 07:39 PM

Hi Tina,

I was able to get your code working by changing this:

<%= images_dir.each { |file|  "hello" } %>
To this:

<% images_dir.each do |file| %>
<%= file %> hello
<% end %>

2007-10-01 04:06 AM

You are so good... :)  It worked for me, too. I was able to take it one step further and print out the actual filenames, too... Now I should be able to finish it and actually display the images.

Thank you!

2007-10-01 09:32 AM


Hello Guest! In order to post you must be an active client with us, please log in or sign up today!