7

I am using Ruby on Rails v3.0.9 and I would like to check if an image (in my case a favicon.ico icon image) is successfully retrieved from a web site and if not I would like to display a custom image.

In order to retrieve the favicon.ico image related to a web site, in my view file I have:

image_tag "#{web_site.link}/favicon.ico", :size => "16x16"

where web_site.link values are something like the followings:

http://stackoverflow.com/
http://www.stackoverflow.com/
http://facebook.com/
...

How to check if an image was found on a web site (maybe using an if ... else ... end statement or performing some HTTP request before to handle favicon images) and how to handle the above scenario?

2 Answers 2

8

Here's how to implement the idea you had in the original question.

The issue with this approach will be that your response times will include however long it takes for the other domain to respond to your request for the image. If that site is having issues, then your page wont load until the request times out.

<%
img_url = 'http://adomain.com/image.jpg'
res = Net::HTTP.get_response(URI.parse(img_url))
img_url = '[my alternate url]' unless res.code.to_i >= 200 && res.code.to_i < 400 #good codes will be betweem 200 - 399
%>

<%=image_tag img_url%>

The jQuery approach is a bit more involved. I'd suggest something along the following:

  • create an <img> tag with a transparent spacer image
  • in the page's javascript run a $.ajax call for the remote image
    • in the success callback replace the <img>'s src with the remote images's url
    • in the failure callback replace the <img>'s src with the fallback image's url

Unfortunately, I don't have time to generate the exact code for this right now.

3
  • "Unfortunately, I don't have time to generate the exact code for this right now." :'-( ... I will try my self...
    – Backo
    Aug 26, 2011 at 14:43
  • Why you use to_f here? Is it possible the status code is a float rather than int?
    – new2cpp
    Jan 4, 2017 at 18:23
  • @new2cpp no idea why I used to_f, editing response Jan 7, 2017 at 18:26
-1

You can't do this with Rails unless you fetch the image server-side before rendering the page. What you could do is fetch it in the client with a JavaScript and if there's trouble retrieving it, then switch to an alternative.

5
  • How can I fetch the image server-side before rendering the page with JavaScript (I am using jQuery 1.6)?
    – Backo
    Aug 26, 2011 at 14:19
  • You've got options in both regards, but the curb gem might be the best option. Also it's either download server-side or client-side with JavaScript, not both. It's just that someone has to verify the link works, you just need to pick who. If you're doing this server-side, you'd probably want to run it as a background process that populates a mapping table.
    – tadman
    Aug 26, 2011 at 14:21
  • Why do you advice me to use the 'curb' gem and not the Ruby standard library?
    – Backo
    Aug 26, 2011 at 14:32
  • BTW: I have also heard of the Typhoeus gem. What about that?
    – Backo
    Aug 26, 2011 at 14:40
  • curb is built on libcurl, and it offers more features than the built in Net::HTTP library. I also think it's easier to use, but there are alternatives all of which do similar things.
    – tadman
    Aug 26, 2011 at 14:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.