Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Ruby-on-Rails and 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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

Ruby-on-Rails and AJAX
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2007-03-27

    Table of Contents:
  • Ruby-on-Rails and AJAX
  • AJAX: an Overview
  • Implementing AJAX
  • Ruby-on-Rails and AJAX

  • 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


    Ruby-on-Rails and AJAX - Ruby-on-Rails and AJAX


    (Page 4 of 4 )

    In real world applications one of the most required functionalities is to populate a combo box based on the data entered in a preceding field such as a text box or another combo box. In this example, I will be developing such a part for an application. This can be used with any application with slight modifications. It contains two components:

    index.rhtml - the view which contains the combo box to be filled and the
                         combo box on the basis of which the filling has to be done

    PopulateController - the controller containing the action required for the
                                     populating of the combo box.

    Here is the index.rhtml:

    <html>
     
    <head>
       
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
       
    <%= javascript_include_tag "prototype" %>
       
    <title>Ajax Rails &amp; Select lists</title>
     
    </head>
     
    <body>
       
    <%= form_remote_tag(:update => "sel_con",:url => {
          :action => :create_select },
          :position => "top",:success => "$('sel_con').innerHTML=''" ) %>
       
    <p>
         
    Please select a sports category:
       
    </p>
       
    <p>
         
    <%= select_tag "categories",
         
    "<option>Team</option><option>Individual</option>" %>
       
    </p>
       
    <div id="sel_con"></div>
        
    <p>
         
    <%= submit_tag "Show Sports" %>
       
    </p>
       
    <%= end_form_tag %>
     
    </body>
    </html>

    The combo box contains two values, team and individual. Based on the selection the second combo has to be populated and shown at the div having the id sel_con. If team is selected by the user, the second combo box would be filled with team events whereas in the case of individual, individual event is populated. The form_remote_tag is passed four parameters: the id of element to be updated, the action to be called, the position in which to place the result and what has to be done if the request is successfully executed. In this case the inner HTML is set to no value i.e. "". Next is the controller:

    class PopulateController < ApplicationController
     
    def index
     
    end

      def create_select

        indArr=["Nordic Skiing", "Inline Skating","Tennis",
          "Triathlon","Road Racing","Figure Skating",
          "Weight Lifting","Speed Skating","Snowboarding"];
       
    teamArr=["Soccer","Basketball","Football","Hockey",
          "Baseball","Lacrosse"];
       
    str="";

        if params[:categories].index('Team') != nil
         
    render :partial => "options",
           
    :locals => { :sports => teamArr,:sptype => "team"}
       
    elsif params[:categories].index('Individual') != nil
         
    render :partial => "options",
           
    :locals => { :sports => indArr, :sptype => "individual" }
       
    else
         
    str="<select id='individual' name='individual'>
              
    <option>unknown</option></select>";
         
    render :text => str;
       
    end

        #end method
      end
     
    #end class definition
    end

    The create_select is the action where the logic for creating and populating the second combo box goes. First it creates two arrays with team and individual sports events. Then based on the parameter received, the combo box is created and populated. This is done in the statement

    render :partial => "options",
    :locals => { :sports => teamArr,:sptype => "team"}

    Now let's look at _options.rhtml which is a partial template i.e. the a piece of HTML code that can be used again and again. Here is the code:

    <select id="<%= sptype %>" name="<%= sptype %>">
    <% sports.each do |sport| %>
    <option><%= sport %></option>
    <% end %>
    </select>

    It first gives the combo box an id, iterates through the array passed and populates the combo box. That's it. This completes the application. Though I had mentioned four libraries, I have introduced only one of them. In the next discussion, I will be focusing on UI using the other three libraries. Till then..


    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.

       · AJAX and Ruby-on-Rails are the current hottest topics in web development. In this...
     

    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-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway