# Default to when missing

**URL:** <https://www.ruby-forum.com/t/default-to-when-missing/84684>\
**Category:** Ruby\
**Created:** [February 6, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/default-to-when-missing/84684 "2007-02-06T23:50:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerryjj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jerryjj](https://www.ruby-forum.com/u/jerryjj)\
**Post date:** [February 6, 2007, 11:50pm UTC](https://www.ruby-forum.com/t/default-to-when-missing/84684/1 "2007-02-06T23:50:27Z")

</div>

On my website, I have a field in the sql server that lists the image  
location/name to display for an item. All is wonderful. How can I have  
the site select an ‘image is no available’ image if the file name it  
is looking for does not exist? Right now, it is just showing the  
alternate text, which is the image file name it is looking for.  
Would it be a big hit on performance to have it check the images dir  
for the file name prior to rendering the page, or should it keep an  
index somewhere of the files that are there, or am I complete off base  
in how to approach this?

---

<div class="post-metadata">

**Author:** ![jerryjj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jerryjj](https://www.ruby-forum.com/u/jerryjj)\
**Post date:** [February 7, 2007, 12:01am UTC](https://www.ruby-forum.com/t/default-to-when-missing/84684/2 "2007-02-07T00:01:27Z")

</div>

On Wed, Feb 07, 2007 at 07:49:20AM +0900, Jerry J. wrote:

> On my website, I have a field in the sql server that lists the image  
> location/name to display for an item. All is wonderful. How can I have  
> the site select an ‘image is no available’ image if the file name it  
> is looking for does not exist?

By configuring your webserver appropriately. You don’t even mention what  
webserver you use, so you’re not going to get a more specific answer  
than  
that here.

However, as an example, if you are running Apache httpd then you can ask  
this question on the Apache httpd mailing list.

> Right now, it is just showing the  
> alternate text, which is the image file name it is looking for.  
> Would it be a big hit on performance to have it check the images dir  
> for the file name prior to rendering the page

Web servers don’t render pages - web browsers do. Web servers response  
to  
HTTP requests, and send back the HTML for the page. What the browser  
does  
with it is its own responsibility.

If the HTML contains a reference to an image, a separate HTTP request is  
made by the browser to retrieve the image. If the image doesn’t exist  
(web  
server returns 404 Not Found) then it’s up to the browser what to do  
next.  
Many display a “broken image” icon.

Brian.

---

<div class="post-metadata">

**Author:** ![jerryjj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jerryjj](https://www.ruby-forum.com/u/jerryjj)\
**Post date:** [February 7, 2007, 3:01am UTC](https://www.ruby-forum.com/t/default-to-when-missing/84684/3 "2007-02-07T03:01:39Z")

</div>

I understand that browsers render the page, I could have worded that  
better. What I was meaning was when a request is sent, would it be a  
serious hit on performance to check for the image file, and if not  
present, default to the ‘no image available’ image. Or would it be  
best to have an index of the image file names and when a request is  
sent to the server, it checks the index, and if the image file is not  
listed, default to the ‘no image available’ image.  
At the moment, I am running Webrick. I am trying to get IIS to work with  
RoR.

---

<div class="post-metadata">

**Author:** ![jerryjj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jerryjj](https://www.ruby-forum.com/u/jerryjj)\
**Post date:** [February 7, 2007, 9:41am UTC](https://www.ruby-forum.com/t/default-to-when-missing/84684/4 "2007-02-07T09:41:04Z")

</div>

On Wed, Feb 07, 2007 at 11:00:38AM +0900, Jerry J. wrote:

> I understand that browsers render the page, I could have worded that  
> better. What I was meaning was when a request is sent, would it be a  
> serious hit on performance to check for the image file, and if not  
> present, default to the ‘no image available’ image.

You mean: when the webserver gets a request for /images/foo.png, and  
foo.png  
does not exist, it returns a default image instead?

Yes this is possible. Under Apache you would use mod\_rewrite, using the  
-f  
test to check if a file exists.

> Or would it be  
> best to have an index of the image file names and when a request is  
> sent to the server, it checks the index, and if the image file is not  
> listed, default to the ‘no image available’ image.

I think you are talking about testing the existence of a file in the  
filesystem, versus testing the existence of a key in a hash. The hash  
lookup  
will be a bit faster, but I don’t think you’ll see much difference in  
practice; if lots of clients are accessing non-existent files, then the  
directory will be cached by the O/S anyway.

> At the moment, I am running Webrick.

Then I guess you can serve /images/ from a small piece of Ruby which  
implements either of the above strategies.

> I am trying to get IIS to work with RoR.

No idea how you would do this with IIS, apart from continuing to map  
/images/\* to a piece of Ruby.

---

<div class="post-metadata">

**Author:** ![jerryjj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jerryjj](https://www.ruby-forum.com/u/jerryjj)\
**Post date:** [February 7, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/default-to-when-missing/84684/5 "2007-02-07T15:37:48Z")

</div>

On Tue, 06 Feb 2007 23:49:20 +0100, Jerry J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Right now, it is just showing the  
> alternate text, which is the image file name it is looking for.  
> Would it be a big hit on performance to have it check the images dir  
> for the file name prior to rendering the page, or should it keep an  
> index somewhere of the files that are there, or am I complete off base  
> in how to approach this?

An index will probably flat-out outperform a system call on access, with  
the cost being in recomputation of said. However, benchmark. A few  
stat()s  
per request probably won’t be a bottleneck anyway.

David V.
