Execute ruby code on click

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

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.

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.