Ruby on Rails Friday, March 2, 2012

Rodrigo Ruiz wrote in post #1049716:
> Hi, I'd like to know how can I do a button that on click with call an
> action, but wont reload the page.

If you're calling back to your same origin (domain) then you can use
XHTTPRequest (a.k.a. AJAX). We almost never use that object directly.
It's usually managed by a JavaScript framework such as jQuery.

Example:
------------------
$(function() {
$('my_button').click(clickHandler);
});

function clickHandler() {
$('target_div').load('http://example.com/posts');
}

This would first wait for the DOM to load (to make sure the button
element is available), then bind a click event to the button element.

Clicking the button calls the handler function, which then uses jQuery
to send a request to a URL that returns an HTML fragment, which finally
gets loaded inside of the target_div.

> Like the "like" button at facebook.

I'm guessing the Facebook Like button has to do some fairly tricky
things to get around the "same origin" policy. It doesn't sound like
that's really what you need.

--
Posted via http://www.ruby-forum.com/.

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