# Execute ruby code on click

**URL:** <https://www.ruby-forum.com/t/execute-ruby-code-on-click/113429>\
**Category:** Rails\
**Created:** [August 31, 2007, 12:08am UTC](https://www.ruby-forum.com/t/execute-ruby-code-on-click/113429 "2007-08-31T00:08:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [August 31, 2007, 12:08am UTC](https://www.ruby-forum.com/t/execute-ruby-code-on-click/113429/1 "2007-08-31T00:08:00Z")

</div>

I have a number of links to external webpages and want to execute some  
Ruby code whenever one of them is clicked, e.g. log which URL was  
clicked how many times. To the user the links should behave as usual  
though.

I’ve been playing around with link\_to\_remote but couldn’t find a  
solution.

Any suggestions?

Max

---

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [August 31, 2007, 2:10am UTC](https://www.ruby-forum.com/t/execute-ruby-code-on-click/113429/2 "2007-08-31T02:10:10Z")

</div>

First of all link\_to\_remote is for sending AJAX requests to the server  
not for sending the user to a remote site.

What you need to do is have your links calls an action in your  
controller. The action will log the click by updating your database,  
then you can redirect the user to the remote site (probably in a new  
browser window by using target = “\_blank”). In any case your  
application will need to initially receive the request so it can  
handle the processing you want. The only affect your user may notice  
is the redirect, but that’s pretty common on the web anyway.

---

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [August 31, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/execute-ruby-code-on-click/113429/3 "2007-08-31T15:02:42Z")

</div>

> First of all link\_to\_remote is for sending AJAX requests to the server  
> not for sending the user to a remote site.

I know, I was trying to have a normal link and let it execute some  
ruby code onclick. So I tried a few things with link\_to and  
link\_to\_remote. But this seems to be impossible, right?

> What you need to do is have your links calls an action in your  
> controller. The action will log the click by updating your database,  
> then you can redirect the user to the remote site (probably in a new  
> browser window by using target = “\_blank”). In any case your  
> application will need to initially receive the request so it can  
> handle the processing you want. The only affect your user may notice  
> is the redirect, but that’s pretty common on the web anyway.

I was trying to avoid redirection, but if that’s the only solution, ok.
