Reputation: 996
Whats the best way to create an image_tag(via the rails helper) that displays another image if the default one is broken instead of text. It doesn't have to support recursion. I just have a default image I want to use if a link is broken.
Upvotes: 2
Views: 990
Reputation: 1
I struggle with similar kind of problem where I have inside table several thumbnail pictures and I needed to checked if that picture is missing then show default image.
For this I use image_tag with helper method. In helper method I check with File.exist? if file exist. If it was then I returned path to image as a string to image_tag else I returned path to default image.
Upvotes: 0
Reputation: 686
This post uses the background-image
CSS property to point to a default image, which would be used if your image isn't found.
Upvotes: 1
Reputation: 160170
You can do this via JS, here shown as jQuery:
$('img').error(function() {
$(this).attr('src', 'missing.png');
});
If it's a specific image you could attach the error handler to just that object.
I don't know of any way to do this via the image_tag
helper; you'd have to make a request from the app to the src URL and check for an error. I'd make sure that's actually what you need to do before going down that road, though, but it depends on your requirements.
Upvotes: 2