Ajax
Be sure to include the JavaScript libraries in the layout:
<%= javascript_include_tag :defaults %>
Linking to Remote Action
<%= link_to_remote "link", :update => 'some_div',
:url => { :action => 'show', :id => post.id } %>
<%= link_to_remote "link", :url => { :action => 'create',
:update => { :success => 'good_div',
:failure => 'error_div' },
:loading => 'Element.show('spinner'),
:complete => 'Element.hide('spinner') } %>Callbacks
:loadingCalled when the remote document is being loaded with data by the browser.
:loadedCalled when the browser has finished loading the remote document.
:interactiveCalled when the user can interact with the remote document, even though it has not finished loading.
:successCalled when the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range.
:failureCalled when the XMLHttpRequest is completed, and the HTTP status code is not in the 2XX range.
:completeCalled when the XMLHttpRequest is complete (fires after success/failure if they are present).
You can also specify reactions to return codes directly:
link_to_remote word,
:url => { :action => "action" },
404 => "alert('Not found...? Wrong URL...?')",
:failure => "alert('HTTP Error ' + request.status + '!')"Ajax Forms
You can create a form that will submit via an
XMLHttpRequest instead of a POST request. The parameters are passed
exactly the same way (so the controller can use the params method to access the parameters). Fallback for non-JavaScript-enabled browsers can be specified by using ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access