# Count views to images

**URL:** <https://www.ruby-forum.com/t/count-views-to-images/139844>\
**Category:** Rails\
**Created:** [June 8, 2008, 2:28am UTC](https://www.ruby-forum.com/t/count-views-to-images/139844 "2008-06-08T02:28:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![procyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@procyon](https://www.ruby-forum.com/u/procyon)\
**Post date:** [June 8, 2008, 2:28am UTC](https://www.ruby-forum.com/t/count-views-to-images/139844/1 "2008-06-08T02:28:49Z")

</div>

Hi,

I want to implement logic in Rail so that I can count number of views  
to an image served by my server. Any suggestions on how to do it ?

How do I intercept request to an image , perform book keeping, then  
redirect the request to the image ?

Thanks!

---

<div class="post-metadata">

**Author:** ![procyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@procyon](https://www.ruby-forum.com/u/procyon)\
**Post date:** [June 8, 2008, 2:38am UTC](https://www.ruby-forum.com/t/count-views-to-images/139844/2 "2008-06-08T02:38:11Z")

</div>

> I want to implement logic in Rail so that I can count number of views  
> to an image served by my server. Any suggestions on how to do it ?
> 
> How do I intercept request to an image , perform book keeping, then  
> redirect the request to the image ?

Images should route straight thru your web server without ever touching  
Rails -  
or especially Ruby!

Ask this question on a forum that covers your web server. Reading its  
access.log  
to see which images are popular might be efficient, or might not!

---

<div class="post-metadata">

**Author:** ![procyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@procyon](https://www.ruby-forum.com/u/procyon)\
**Post date:** [June 8, 2008, 11:09am UTC](https://www.ruby-forum.com/t/count-views-to-images/139844/3 "2008-06-08T11:09:00Z")

</div>

Well actually i need to do more than counting the views. I need to log  
information about the client and perform some tasks before serving the  
image. I need to also do several database transactions.

Actually I can make the problem simple. The request can come to my  
regular rails function. Thus, any request which comes to  
[http://myserver.com/controller/test](http://myserver.com/controller/test) should direct request to  
[http://myserver.com/images/my\_precious.jpg](http://myserver.com/images/my_precious.jpg)

How do I do that ?

Thanks

---

<div class="post-metadata">

**Author:** ![procyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@procyon](https://www.ruby-forum.com/u/procyon)\
**Post date:** [June 8, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/count-views-to-images/139844/4 "2008-06-08T14:13:57Z")

</div>

On 8 Jun 2008, at 10:08, procyon wrote:

> How do I do that ?

send\_data, send\_file, xsendfile

Fred

---

<div class="post-metadata">

**Author:** ![procyon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@procyon](https://www.ruby-forum.com/u/procyon)\
**Post date:** [June 8, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/count-views-to-images/139844/5 "2008-06-08T18:16:27Z")

</div>

How about something like:

def my\_action

# some db transactions and other tasks

redirect\_to ‘/images/my\_image.jpg’  
end

then put the image in /public/images/my\_image.jpg

This will prevent rails from having to serve the image (which isn’t  
terribly efficient). The only problem is that anyone who directly  
visits ‘[http://www.example.com/images/my\_image.jpg](http://www.example.com/images/my_image.jpg)’ will see the image  
and all your db transactions and other tasks will be skipped.

I hope that helps,  
Paul
