Home arrow Ruby-on-Rails arrow Page 4 - Dropping and Sorting with AJAX and script.aculo.us
RUBY-ON-RAILS

Dropping and Sorting with AJAX and script.aculo.us


In this conclusion to a three-part series that explains how to use script.aculo.us on Rails, you'll learn dropping and sorting. This article is excerpted from chapter four of Ajax on Rails, written by Scott Raymond (O'Reilly, 2007; ISBN: 0596527446). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 8
November 29, 2007
TABLE OF CONTENTS:
  1. · Dropping and Sorting with AJAX and script.aculo.us
  2. · Droppables with Ajax
  3. · Sortables
  4. · Ajax-enabled sortables

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Dropping and Sorting with AJAX and script.aculo.us - Ajax-enabled sortables
(Page 4 of 4 )

As with droppables, the sortable_element helper also can take all the familiar Ajax options that link_to_remote provides. By default, when an Ajax call is created, the action called gets the serialized sortable elements as parameters. To work, the IDs of the sortable elements should follow the naming convention used bySortable.serialize: the unique part of the ID should be at the end, preceded by an underscore. Soitem_1,person_2, and_3would make good IDs, butitem1,2_personand3would not. For example:

  <ul id="listAjax">
    <li id="item_1">Buy milk</li>
    <li id="item_2">Take out trash</li>
    <li id="item_3">Make first million</li>
  </ul>

  <%= sortable_element :listAjax,
       :url      => { :action => 'repeat' },
       :complete => "alert(request.responseText);" %>

In the example, reordering the list triggers an Ajax call to therepeataction, which gets alistAjaxarray parameter containing the IDs of the sortable elements, in the current order. To see this in action, define arepeataction to echo back the parameters it receives, like this:

  def repeat
   
render :text => params.inspect
  end

For a real-world example of creating sortables and handling reordering on the server side, see the Review Quiz example application in Example A.

Summary

This chapter introduced the major features of script.aculo.us—specifically, those features of the library that have corresponding Rails helpers. Those features fall into two main categories: visual effects and drag and drop. The library has even more to offer and is fully dissected in Chapter 11.

In the next chapter, we’ll explore the crown jewel of Ajax on Rails: Ruby-generated JavaScript (RJS).


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials