# Link as a link AND as a function

**URL:** <https://www.ruby-forum.com/t/link-as-a-link-and-as-a-function/185140>\
**Category:** Rails\
**Created:** [March 14, 2010, 11:22pm UTC](https://www.ruby-forum.com/t/link-as-a-link-and-as-a-function/185140 "2010-03-14T23:22:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [March 14, 2010, 11:22pm UTC](https://www.ruby-forum.com/t/link-as-a-link-and-as-a-function/185140/1 "2010-03-14T23:22:02Z")

</div>

Hi there,

I am relatively new to Rails and I am trying to do a fairly common task.  
I have a page that displays a series of links that go to offsite URLS.  
Next to each link is a counter that shows how many times that link has  
been clicked-through. I would like to be able to have the user click on  
a the link, be directed to the links address, AND at the same time, have  
a function create and add a “click-through” to the “clicks” database.

I was reading about the link\_to\_function helper but that doesn’t seem to  
be quite what I want, but then again, I may not be understanding it  
completely.

Does anyone have any method for doing anything similar in their apps? I  
would love an help or advice.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [March 15, 2010, 3:04am UTC](https://www.ruby-forum.com/t/link-as-a-link-and-as-a-function/185140/2 "2010-03-15T03:04:43Z")

</div>

Quoting M. Murillo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> be quite what I want, but then again, I may not be understanding it  
> completely.

Link to a function like the following. It makes a GET request to the  
server  
and opens a new window to the url. The link\_to also follows.

HTH,  
Jeffrey

\<%= link\_to\_function truncate(article.title, 60),  
“clickThru(‘#{article.url}’,‘#{article[:id]}’, ‘click’)”,  
:title =\> article.feed.title, :href =\> article[:id] %\>

function clickThru(url, id, verb) {  
new Ajax.Request(‘/articles/’+id+‘/’+verb,  
{asynchronous:true, evalScripts:true, method:‘get’});  
window.open(url);  
}

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [March 16, 2010, 4:09am UTC](https://www.ruby-forum.com/t/link-as-a-link-and-as-a-function/185140/3 "2010-03-16T04:09:58Z")

</div>

Jeffrey L. Taylor wrote:

> Quoting M. Murillo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):  
> \<%= link\_to\_function truncate(article.title, 60),  
> “clickThru(‘#{article.url}’,‘#{article[:id]}’, ‘click’)”,  
> :title =\> article.feed.title, :href =\> article[:id] %\>
> 
> function clickThru(url, id, verb) {  
> new Ajax.Request(‘/articles/’+id+‘/’+verb,  
> {asynchronous:true, evalScripts:true, method:‘get’});  
> window.open(url);  
> }

Thank you Jeffrey. I played around with it a bit and was wondering how  
the function intends to post to the clicks database. Is it by using  
that “id” =\> “click”? Or does “click” in your example refer to a method  
in a controller?

Thanks again Jeffrey.

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [March 17, 2010, 3:45pm UTC](https://www.ruby-forum.com/t/link-as-a-link-and-as-a-function/185140/4 "2010-03-17T15:45:50Z")

</div>

Quoting M. Murillo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > window.open(url);  
> > }
> 
> Thank you Jeffrey. I played around with it a bit and was wondering how  
> the function intends to post to the clicks database. Is it by using  
> that “id” =\> “click”? Or does “click” in your example refer to a method  
> in a controller?

Sorry for the slow reply. I have been at SxSW Interactive for the last  
5  
days.

This code is copied from my app, it will need some editing for your app.  
Ajax.request() is in the Prototype Javascript library, it makes a  
request to  
the server. The first parameter is the URL to call, the rest is copied  
from  
the code generated by link\_to\_remote. evalScripts:true indicates  
Javascript  
is expected in the reply and should be evaluated, method:‘get’ indicates  
to  
make a HTTP GET request instead of the default POST. The action on the  
server  
calls the model(s) to update the database.

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [March 22, 2010, 2:37am UTC](https://www.ruby-forum.com/t/link-as-a-link-and-as-a-function/185140/5 "2010-03-22T02:37:51Z")

</div>

Gotcha! Perfect. Thank you Jeffrey.

I got it to work with the following.

In my view:

\<%= link\_to\_function truncate(link.name, 60),  
“clickThru(’#{link.link}’,’#{link[:id]}’, ‘votes’)”,  
:title =\> link.name, :href =\> link.link %\>

In my application.js

function clickThru(url, id, verb) {  
new Ajax.Request(’/links/’+id+’/’+verb,  
{asynchronous:true, evalScripts:true, method:‘post’});  
window.open(url);  
}

Thank you so much! This was awesome and your advise came at the end of  
maybe 5 hours of internet searching.
