Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - Introduction to Ruby on Rails with Ajax
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Introduction to Ruby on Rails with Ajax
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-07-26

    Table of Contents:
  • Introduction to Ruby on Rails with Ajax
  • Playing a Slideshow
  • Using Drag-and-Drop to Reorder Slides
  • Using Drag-and-Drop to Reorder Slides continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introduction to Ruby on Rails with Ajax - Using Drag-and-Drop to Reorder Slides


    (Page 3 of 4 )

    The scaffolding we have for editing a slideshow shows just the slideshow attributes that are stored directly in the slideshows table: the slideshows name and the date on which it was created. The most important part is missing: the photos that are part of the slideshow!

    By now, you've probably realized that this is because the scaffolding code deals with only one database table: the slideshows table. The relationship data about which photos are assigned to a slideshow and their order in the slideshow are stored in the slides table. Scaffolding does not handle relationships, so you have to write the code to edit this relationship data.

    We're going to display a list of thumbnails of all the photos that are in a slideshow, and then let the user reorder them using drag-and-drop. If you've had to struggle through implementing drag-and-drop before, you're not going to believe how easy this is going to be. Here's a hint: this will take a total 34 additional lines of Ruby, CSS, and RHTML template!

    Let's start by reviewing the current implementation of the edit action in the slideshow controller:

      def edit
        @slideshow = Slideshow.find(params[:id])
      end

    This action expects to find the ID of the slideshow to edit passed in as the id parameter, which is normally decoded from the URL. You find the slideshow with that ID and assign that slideshow object to the instance variable @slideshow, so that it can be accessed in the view template.

    That is really all that's needed here, so you won't have to add any code to this method. The changes will start with the edit view template, so edit the template photos/app/views/slideshows/edit.rhtml and make it look like this (the changes are in bold):

      <h1>Editing slideshow</h1>

      <%= link_to 'Play this Slideshow',
                  :action => 'show', :id => @slideshow %>

      <div id='slideshow-contents'>
        <%= render :partial => 'show_slides_draggable' %>
      </div>
     
    <div id='slideshow-attributes'>
        <%= start_form_tag :action => 'update', :id => @slideshow %>
         
    <%= render :partial => 'form' %>
         
    <%= submit_tag 'Save Attributes' %>
        <%= end_form_tag %>
     
    </div>

    Notice that the existing <%= render :partial => 'form' %> is wrapped in a <div> tag with an id attribute of slideshow-attributes. You will use this name in one of your CSS files to control how this section is displayed.

    There is also a completely new section that displays thumbnails of the photos in the slideshow:

      <div id='slideshow-contents'>
         <%= render :partial => 'show_slides_draggable' %>
      </div>

    This code also uses a <div> tag with an id attribute, for the same reason: to use a CSS file to control its appearance. This div also renders a new partial view template named show_slides_draggable, which we will create next.

    Create the file photos/app/views/slideshows/_show_slides_draggable.rhtml with the following contents:

      <ol id='sortable_thumbs'>
         <% for slide in @slideshow.slides %>
            <li id='thumbs_<%= slide.id %>' class='slides'>
               <%= thumbnail_tag slide %>
            </li>
        
    <% end %>
      </ol>

      <%= sortable_element('sortable_thumbs',
                           :url => {:action => 'update_slide_order'}) %>

    The first part is pretty standard stuff. We're creating an HTML ordered list, in which each list item is a thumbnail image of one of the photos in the slideshow (note that the thumbnail_tag helper function that was created earlier). However, it's the last two lines that do the heavy lifting.

    sortable_element is a helper function that generates the JavaScript code that turns our list into a user-sortable, drag-and-drop-capable list. It wraps this list an HTML form, and the :url option specifies the URL to post to the server whenever the user changes the order of the list. In this case, it calls the action method update_slide_order in our slideshow controller. This call works in the background using an Ajax call.

    More Ruby-on-Rails Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Ruby on Rails: Up and Running," published...
       · it would be helpful for people new to rails (like myself) if this widely distributed...
     

    Buy this book now. This article is excerpted from chapter six of the book Ruby on Rails: Up and Running, written by Bruce A. Tate and Curt Hibbs (O'Reilly, 2006; ISBN: 0596101325). Check it out today at your favorite bookstore. Buy this book now.

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek