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:
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.
I think you want @style.style_image.path
instead of .url
.
Print it out to be sure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With