views:

860

answers:

2

Hi,

I am trying to check if a file exists in my rails application. I am running ruby 1.8.6 and rails 2.1.2 with windows XP.

So, the problem is that the FileTest.exists? method doesn't seem to be working. I have simplified the code to this point :

if FileTest.exists?("/images/header.jpg")
      render :text => "yes"
else  
      render :text => "no <img src='/images/header.jpg' />"
end

If I do that the system displays "no" and then includes the image that displays correctly because /images/header.jpg exists.

I tried FileTest.exists?, FileTest.exist?, File.exists?, File.exist? and nothing seems to be working.

What am I doing wrong ? Thanks

+4  A: 

I'm guessing it's because you're asking whether a file "header.jpg" exists in a directory "images" off of the root directory for your system (which on Windows I'd assume is "c:\"). Try putting the full path (from the filesystem root) to the "/images" directory rather than the URL path.

In particular, as pointed out by @Brian, you should use:

FileTest.exists?(RAILS_ROOT + "/images/header.jpg")
Rick Copeland
It worked using both of your comment and Brian's.I now do : unless FileTest.exists?(RAILS_ROOT+"/public/data/songs/"+recording.file_name) and it's working fine.
marcgg
+2  A: 

Add RAILS_ROOT to the filename that you're checking before calling exists?

Brian
would you mind if I incoporated your answer into mine?
Rick Copeland