Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails File.exist? yields false when file is present

I've found a few posts here regarding File.exists? in a Rails app but trying the solutions have not helped, I'm fairly new so I must be doing something dumb.

I'm using:

  • Rails 3.2.11
  • Ruby 1.9.3
  • Paperclip for the file mentioned below
  • ActiveAdmin to upload the file mentioned below
  • Working in development environment
  • Assets not precompiled

I have a model "style" and it has an image attachment, I can render the image with

<%= image_tag(@style.style_image) %>

and it works just fine.

In short, I want to check if the file image is actually there in the folder it should be in - I don't want to use @style.style_image.present? for checking images because that just checks the db record. I want to use File.exist? to see if there's actually a file for @style.style_image.

So in my view file, I have the code

<% if File.exist?(@style.style_image.url) %>
The image exists.
<% else %>
The image is not here.
<% end %>

And it always prints "the image is not here" when I load the page. Directly below I am displaying my image using image_tag, so I know for fact that the image is there.

I've also tried

<% if File.exist?(Rails.root + @style.style_image.url) %>

with no luck. I also tried using FileTest.exist?, FileTest.exists?, and File.exists? but none will tell me true when the image is definitely there.

Is there something I'm missing? Any guidance would be appreciated very much. I'm only a few months into Ruby and Rails so I'm probably missing something dumb.

like image 758
Tom Netzband Avatar asked Mar 15 '13 02:03

Tom Netzband


Video Answer


1 Answers

I think you want @style.style_image.path instead of .url.

Print it out to be sure.

like image 154
Jorge Israel Peña Avatar answered Nov 16 '22 04:11

Jorge Israel Peña