# execute ruby code on click

**URL:** <https://discuss.rubyonrails.org/t/execute-ruby-code-on-click/17604>\
**Category:** rubyonrails-talk\
**Created:** [August 30, 2007, 10:07pm UTC](https://discuss.rubyonrails.org/t/execute-ruby-code-on-click/17604 "2007-08-30T22:07:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max1](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Max1](https://discuss.rubyonrails.org/u/Max1)\
**Post date:** [August 30, 2007, 10:07pm UTC](https://discuss.rubyonrails.org/t/execute-ruby-code-on-click/17604/1 "2007-08-30T22:07:16Z")

</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:** ![GoodbyeJava](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@GoodbyeJava](https://discuss.rubyonrails.org/u/GoodbyeJava)\
**Post date:** [August 30, 2007, 11:03pm UTC](https://discuss.rubyonrails.org/t/execute-ruby-code-on-click/17604/2 "2007-08-30T23:03: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.

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.

Max wrote:

---

<div class="post-metadata">

**Author:** ![Max1](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Max1](https://discuss.rubyonrails.org/u/Max1)\
**Post date:** [August 31, 2007, 12:59pm UTC](https://discuss.rubyonrails.org/t/execute-ruby-code-on-click/17604/3 "2007-08-31T12:59:41Z")

</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.
