Ajax on Rails
Pages: 1, 2
Using form_remote_tag
The form_remote_tag() helper is similar to
link_to_remote() except that it also sends the contents of an HTML
form. This means that the action handler can use user-entered data to formulate
the response. This example displays a web page that shows a list and an
Ajax-enabled form that lets users add items to the list.
My view template (index.rhtml) looks like:
<html>
<head>
<title>Ajax List Demo</title>
<%= javascript_include_tag "prototype" %>
</head>
<body>
<h3>Add to list using Ajax</h3>
<%= form_remote_tag(:update => "my_list",
:url => { :action => :add_item },
:position => "top" ) %>
New item text:
<%= text_field_tag :newitem %>
<%= submit_tag "Add item with Ajax" %>
<%= end_form_tag %>
<ul id="my_list">
<li>Original item... please add more!</li>
</ul>
</body>
</html>
Notice the two parts in bold. They define the beginning and end of the form.
Because the form started with form_remote_tag() instead of
form_tag(), the application will submit this form using
XMLHttpRequest. The parameters to form_remote_tag() should look
familiar:
- The update parameter specifies the id of the DOM element with content to
update by the results of executing the action--in this case,
my_list. - The url parameter specifies the server-side action to call--in this case,
an action named
add_item. - The position parameter says to insert the returned HTML fragment at the top
of the content of the
my_listelement--in this case, a<ul>tag.

Figure 4. Before adding any items
My controller class looks like:
class ListdemoController < ApplicationController
def index
end
def add_item
render_text "<li>" + params[:newitem] + "</li>"
end
end
The add_item action handler constructs an HTML list item
fragment containing whatever text the user entered into the
newitem text field of the form.

Figure 5. After adding several new list items
Using Observers
Rails lets you monitor the value of a field and make an Ajax call to an action handler whenever the value of the field changes. The current value of the observed field is sent to the action handler in the post data of the call.
A very common use for this is to implement a live search:
<label for="searchtext">Live Search:</label>
<%= text_field_tag :searchtext %>
<%= observe_field(:searchtext,
:frequency => 0.25,
:update => :search_hits,
:url => { :action => :live_search }) %>
<p>Search Results:</p>
<div id="search_hits"></div>
This code snippet monitors the value of a text field named
searchtext. Every quarter of a second, Rails checks the field for
changes. If the field has changed, the browser will make an Ajax call to the
live_search action handler, displaying the results in the
search_hits div.
You can see an actual demonstration of this live search on my weblog. The search box is in the upper-right corner. Try typing enterprise or rails and
see what you get.
To Use or Not Use (Ajax, That Is)
When you use Ajax techniques to update portions of a web page, the user gains responsiveness and fluidity. However, the user also loses the ability to bookmark and to use the browser's back button. Both of these drawbacks stem from the same fact: the URL does not change because the browser has not loaded a new page.
Don't use Ajax just because it's cool. Think about what makes sense in your web app's user interface.
For example, if a web page displays a list of accounts with operations on the displayed list like adding, deleting, and renaming accounts, these are all good candidates for Ajax. If the user clicks on a link to show all invoices that belong to an account, that's when you should display a new page and avoid Ajax.
This means that the user can bookmark the accounts page and invoices page, and use the back and forward buttons to switch between them. The user can't bookmark the operations within one of these lists or use the back button to try to undo an operation on the list (both of which you would probably want to prevent in a traditional web app, as well).
Odds 'n' Ends
I'd like to bring a couple of really cool things to your attention, but I won't be going into any detail.
Web pages that upload files are often frustrating to users because the user receives no feedback on the status of the upload while it progresses. Using Ajax, you can communicate with the server during the upload to retrieve and display the status of the upload. Sean Treadway and Thomas Fuchs have implemented a live demonstration of this using Rails and a video on how to implement it.
The Prototype JavaScript library that Rails uses also implements a large number of visual effects. The Effects Demo Page has a live demonstration of these effects along with JavaScript calls to use them
You can find more detailed information about these and other Ajax features of Rails in Chapter 18 of Agile Web Development with Rails.
Parting Thoughts
The Web has come a long way since the days of isolated web sites serving up static pages. We are slowly moving into a new era where sites are dynamically interconnected, web APIs allow us to easily build on top of existing services, and the web user interface is becoming more fluid and responsive. Ajax not only plays an important role in this emerging Web 2.0 saga, but also raises the bar on what people will consider to be an acceptable web application.
By all rights, adding complex Ajax features to a web application should be a lot of extra work, but Rails makes it dead simple.
Resources
Web sites
- Official Ruby home page
- Official Ruby on Rails home page
- Rails Ajax API
- Download the Rails source code used to create the screenshots in this article.
Mailing lists
Curt Hibbs has been a consultant to well-known companies like Hewlett Packard, Intuit, Corel, WordStar, Charles Schwab, Vivendi Universal, and more. He now works as a Senior Software Engineer for The Boeing Company in St. Louis.
Return to ONLamp.com.
-
AJAX with XML response ...
2009-05-21 08:29:17 Tisha2kool [View]
-
example
2009-04-15 00:50:44 Rafa1970 [View]
-
Live Search
2006-11-20 09:51:22 Calleja [View]
-
how to load xmls in rubyonrails
2006-08-24 05:20:54 xmls [View]
-
Def index...needed?
2006-07-31 11:41:07 Damaris [View]
-
Problems with draggable_element
2006-04-21 04:28:40 pjazzlg [View]
-
Problems with draggable_element
2006-05-03 06:38:39 kysen [View]
-
Problems with draggable_element
2006-05-04 01:16:37 kysen [View]
-
Problems with draggable_element
2006-04-21 06:06:40 Curt Hibbs |
[View]
-
correct code
2006-04-19 03:05:46 gvlx [View]
-
correct code
2006-04-19 03:09:48 gvlx [View]
-
correct code
2006-04-19 03:02:21 gvlx [View]
-
correct code
2006-04-19 03:06:29 gvlx [View]
-
Live Search Example
2006-02-17 03:01:04 vadhavane_anil [View]
-
Live Search Example
2006-04-19 02:09:52 a_shev [View]
-
Live Search Example
2006-02-17 03:33:36 Curt Hibbs |
[View]
-
AJAX remote method
2006-02-12 16:48:31 lzaman [View]
- Trackback from http://hips.ifi.uio.no:8080/confluence/display/RandD/Ruby+on+Rails
Ruby on Rails
2005-11-02 01:52:28 [View]
- Trackback from http://hips.ifi.uio.no:8080/confluence/display/RandD/Ruby+on+Rails
Ruby on Rails
2005-11-01 06:46:48 [View]
- Trackback from http://hips.ifi.uio.no:8080/confluence/display/RandD/Ruby+on+Rails
Ruby on Rails
2005-10-27 03:48:54 [View]
- Trackback from http://hips.ifi.uio.no:8080/confluence/display/RandD/Ruby+on+Rails
Ruby on Rails
2005-10-26 03:41:12 [View]
-
static class variables in the controller
2005-10-23 15:43:18 lada77@yahoo.com [View]
-
still confused
2005-09-19 11:33:10 rhubarbo [View]
-
still confused
2005-09-19 11:51:13 Curt Hibbs |
[View]
-
can ajax be used for updating two or more div's simultaniously
2005-09-12 12:12:58 viper_again [View]
-
can ajax be used for updating two or more div's simultaniously
2006-09-14 09:22:52 EpsilonsDad [View]
-
can ajax be used for updating two or more div's simultaniously
2005-09-12 12:26:50 Curt Hibbs |
[View]
-
can ajax be used for updating two or more div's simultaniously
2005-09-12 22:09:57 viper_again [View]
-
can ajax be used for updating two or more div's simultaniously
2005-09-13 04:01:28 Curt Hibbs |
[View]
-
can ajax be used for updating two or more div's simultaniously
2005-09-14 08:20:34 viper_again [View]
-
confused about ajax
2005-08-26 10:36:06 rhubarbo [View]
- Trackback from http://jcooney.net/archive/2005/08/21/6593.aspx
Ted Neward -
2005-08-21 06:40:49 [View]
- Trackback from http://www.piersharding.com/blog/archives/2005/08/sap_on_rails_1.html
SAP on Rails
2005-08-03 01:04:58 [View]
- Trackback from http://blogs.klmn.net/gunbuster/?p=35
Ajax on Rails
2005-07-21 10:37:24 [View]
- Trackback from http://townx.org/?q=node/135
Ruby on Rails on XAMPP with FastCGI for Ubuntu Hoary
2005-07-19 07:06:05 [View]
-
Link to "Rails Ajax API" is broken
2005-07-06 12:18:53 Solace [View]
-
access from controller for :searchtext
2005-06-28 19:42:26 vcosby [View]
-
access from controller for :searchtext
2005-07-27 07:33:31 k.y.l.e [View]
-
access from controller for :searchtext
2005-06-29 05:28:27 Curt Hibbs |
[View]
-
access from controller for :searchtext
2005-06-29 21:43:10 vcosby [View]
- Trackback from http://bbuchs.f2o.org/archives/2005/06/23/ajax-ified_weblog/
Ajax-ified Weblog
2005-06-24 06:25:31 [View]
- Trackback from http://www.dodoskido.com/archives/001809_ajax_on_rails.html
Ajax on Rails
2005-06-24 05:37:52 [View]
- Trackback from http://weblogs.rd.ap.org/CS/blogs/michael_welles/archive/2005/06/13/386.aspx
Ajax on Rails...
2005-06-13 12:40:18 [View]
- Trackback from http://www.rubick.com/blogger/one-entry?entry_id=11867
How Rails uses Ajax
2005-06-13 09:27:38 [View]
-
Oops!
2005-06-11 09:01:29 geoffleach [View]
-
Oops!
2005-06-11 10:16:02 Robby Russell |
[View]
-
hosting platforms
2005-06-10 07:15:29 msporleder [View]
-
hosting platforms
2006-03-01 00:09:19 TechNeck [View]
-
hosting platforms
2005-07-08 17:50:32 ritcheyer [View]
-
hosting platforms
2005-06-10 16:50:17 Davidb834 [View]
-
hosting platforms
2005-06-10 12:01:46 bsw1971 [View]
-
hosting platforms
2005-06-13 06:28:23 amitsnyderman [View]
-
hosting platforms
2005-06-10 08:43:17 Robby Russell |
[View]
-
hosting platforms
2007-08-08 17:43:22 panerai25 [View]
-
hosting platforms
2005-06-10 07:51:12 Curt Hibbs |
[View]
-
*** AJAX Example Server is Down ***
2005-06-10 06:34:21 Curt Hibbs |
[View]
-
*** AJAX Example Server is Down ***
2005-06-10 17:04:17 Curt Hibbs |
[View]
-
*** AJAX Example Server is Down ***
2007-05-30 08:12:21 profesjonalna [View]
- Trackback from http://www.approachingnormal.com/articles/2005/06/10/ajax-on-rails-article
Ajax on Rails article
2005-06-10 04:55:19 [View]
-
AJAX super not impressive
2005-06-09 23:28:13 hardy [View]
-
AJAX super not impressive
2005-06-10 04:24:00 Curt Hibbs |
[View]
-
Obscure?
2005-06-09 20:32:33 IDunno [View]