# Increment counter on download

**URL:** <https://www.ruby-forum.com/t/increment-counter-on-download/173910>\
**Category:** Rails\
**Created:** [August 18, 2009, 5:24am UTC](https://www.ruby-forum.com/t/increment-counter-on-download/173910 "2009-08-18T05:24:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [August 18, 2009, 5:24am UTC](https://www.ruby-forum.com/t/increment-counter-on-download/173910/1 "2009-08-18T05:24:29Z")

</div>

i am offering my site visitors a download of zip file. when someone  
downloads it, i want to increment a counter and update db. how do i do  
that?

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [August 18, 2009, 5:29am UTC](https://www.ruby-forum.com/t/increment-counter-on-download/173910/2 "2009-08-18T05:29:02Z")

</div>

Rails L. wrote:

> i am offering my site visitors a download of zip file. when someone  
> downloads it, i want to increment a counter and update db. how do i do  
> that?

I should think it would be obvious – just put an increment routine in  
the action that generates the zip file. Or if it’s a static file, use  
your Web server’s access logs.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [August 18, 2009, 6:21am UTC](https://www.ruby-forum.com/t/increment-counter-on-download/173910/3 "2009-08-18T06:21:50Z")

</div>

I am interested in showing the no. of times this file has been  
downloaded by updating a text field/area. So using webserver log files  
may not help.

I am looking for the possibility of using link\_to. When clicked, it  
should download the file and update the db in its onclick event.

not sure how to do that/

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [August 18, 2009, 9:26am UTC](https://www.ruby-forum.com/t/increment-counter-on-download/173910/4 "2009-08-18T09:26:01Z")

</div>

On 18 Aug 2009, at 06:24, Marnen Laibow-Koser wrote:

> > I am looking for the possibility of using link\_to. When clicked, it  
> > should download the file and update the db in its onclick event.
> > 
> > not sure how to do that/
> 
> Apparently you didn’t fully read my first message. Go back and do  
> that,  
> since I explain it there. (Hint: onclick doesn’t come into it.)

In case your penny doesn’t drop:

User clicks link -\> Serverside method “download” is triggered -\>  
“download” methods does downloads = downloads + 1, save to db, and  
then return file to download

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [August 18, 2009, 10:36am UTC](https://www.ruby-forum.com/t/increment-counter-on-download/173910/5 "2009-08-18T10:36:51Z")

</div>

Thanks Peter. Exactly thats what I have just did using increment.

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [August 18, 2009, 6:24am UTC](https://www.ruby-forum.com/t/increment-counter-on-download/173910/6 "2009-08-18T06:24:21Z")

</div>

Rails L. wrote:  
[…]

> I am looking for the possibility of using link\_to. When clicked, it  
> should download the file and update the db in its onclick event.
> 
> not sure how to do that/

Apparently you didn’t fully read my first message. Go back and do that,  
since I explain it there. (Hint: onclick doesn’t come into it.)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
