There is a path on a local machine (UNIX) like:

/system/site/photos/000/342/small/12104_%281%29.jpg?1468158362 

I try to check if the file exists, but all the options that I found in Google or the documentation always give out false .

  small_img = photo.url(:small) unless File.file?(:small) small_img = photo.url(:original) end render partial: 'common/image_preview', locals: { original: photo.url, thumb: photo.url(:thumb), medium: small_img } 
  • And if you go with the help of cd on the file system, what is the full path to this file? Is he even there? - D-side
  • one
    I'm sorry, but I'm afraid to even imagine how this should work: File.file?(:small) . In general, it is very similar to Paperclip . If you have guessed, then you do not need to reinvent the wheel. Need to see the documentation. Everything is already invented. And yes, it is doubtful to use the url as a path to the file. At a minimum, there are methods that allow you to get exactly the path in the file system. - anoam
  • The file is physically there, if I just try to issue this URL without checking, everything works. But I have many objects in the array that do not have these images at all. - Stanislav Ilyin
  • @anoam What bike are we talking about? I just need to check the file for <code> File :: file? File.file? (File_name) => true or false Returns true if there is something in the documentation or I’m looking for something wrong or do something - can be a link or a key phrase? - Stanislav Ilyin
  • @ StanislavIlyin, that's about it. Paperclip has a native method for checking the existence of a file. Here it should be used. Moreover, its description on the main page of the repository is very strange that you did not notice it. - anoam

1 answer 1

For checking the existence of a file, the methods of class File are intended

 File.file? '/path/to/file.png' File.exist? '/path/to/file.png' 

file? checks the existence of the file and that it is the file, while exist? checks for the existence of both the file and directories and links.

Note that the file path is passed as an argument as a string. In your example, the :small symbol is passed - this is not true, the File Ruby class does not know anything about Rails and relations in your application, it is waiting for the path, it doesn’t have enough style, as in the case of the url method of your model (which knows everything about the file) .