

AJAX IN RAILS

IN THIS CHAPTER
What is Ajax :
AJAX is not a new programming language. It is one type way to use existing standards. It helps to create better, faster, and more user friendly your web applications. AJAX stands for Asynchronous JavaScript And XML.
Features of AJAX :
Advantage of AJAX :
Rails and AJAX :
Rails have a simple, consistent model for implements Ajax operations. Once the browser has displayed the initial web page, different user actions cause it to display a new web page or trigger an Ajax operation :
Helpers methods :
In Rails has several helper methods for implementing Ajax in view's templates. The most common helpers methods in Rails are :
Prototype :
form_remote_for
form_remote_tag
link_to_remote
observe_field
observe_form
periodically_call_remote
remote_form_for
submit_to_remote
Scriptaculous :
draggable_element
drop_receiving_element
sortable_element
visual_effect
RJS :
hide
insert_html
remove
replace
replace_html
show
toggle
It has different types of parameter :
| :loading: | Called when the remote data is being loaded by the browser. |
|
| :loaded: | Called when the browser has completed receiving the remote document. |
|
| :interactive: | Called when the user can interact with the remote data, even though it has not finished loading. |
|
| :success: | Called when the XMLHttpRequest is successfully completed. |
|
| :failure: | Called when the XMLHttpRequest is failed. | |
| :complete: | Called when the XMLHttpRequest is complete whether it was successful or failed. |
The other helpers methods are :
Example :
This is simple example
Welcome to simple Ajax application
Click here
the new text will appear here
If you click the click here portion the next action will appear here without refreshing the page.
Welcome to simple Ajax application
Click here
this text is downloaded using ajax
The Time is :Fri Jan 23 13:59:02 +0530 2009
The code of two view page are :
In Index.rhtml
<html >
<head>
<title> simple Ajax application</title>
<%= javascript_include_tag "prototype" %>
</head>
<body>
<h2> Welcome to simple Ajax application</h2>
<p>
<%= link_to_remote("click here",:update => "displayDiv", :url => {:action => :replacer})%>
<div id = "displayDiv" >the new text will appear here</div>
</p>
</body>
</html>
In replacer.rhtml
this text is downloaded using ajax
The Time is :<%= Time. now %>
