rubyonrailsin

A Ruby and Rails talk

Sunday, March 14, 2010


Re: [Rails] Link as a link AND as a function

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

Quoting Michael Murillo <lists@ruby-forum.com>:
> 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.
>

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);
}

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive