JavaScript
  Home arrow JavaScript arrow Page 3 - Dynamically Generate a Selection List in a...
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? 
JAVASCRIPT

Dynamically Generate a Selection List in a Rails Template
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-07-31

    Table of Contents:
  • Dynamically Generate a Selection List in a Rails Template
  • Work with Ajax and Ruby on Rails
  • Rendering the HTTP Response
  • A Little Partial Pizzazz

  • 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


    Dynamically Generate a Selection List in a Rails Template - Rendering the HTTP Response


    (Page 3 of 4 )

    How is that response rendered, anyway? Let’s look at that part of the controller object:

      class HacksController < 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

    This controller object stores two Rubyarrays in the variablesindArrandteamArr (these values could alternatively be generated from a database). Remember the web page’s existingselectlist that gives the user a choice of Team or Individual? This is aselectelement with thename categories. The browser submits this value as a form parameter to the Rails action. The code uses the syntaxparams[:categories] to get this parameter’s value. Then things get interesting, in a Rails kind of way. The server still has to provide an HTTP response to the Ajax request object.

    The action uses the RoRrender()method to send the HTML for aselectlist back to our application, with thearrayvalues as theselect list contents:

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

    render()specifies in its first parameter apartialnamedoptions. In Rails lingo, apartialis just a template that contains a chunk of content that can be used over and over again—for example, one or a few HTML tags. Using the Rails naming convention, thepartial(really a text file) is placed in the app/ views/hacks directory with its name preceded by an underscore, as in _options. rhtml. So, in plain English, the method call declares, “Render the response as
    the_options partialcontent, and hand thepartialtwo local variables,:sportsand:sptype.”

    Don’t forget the : before the variable names!

    More JavaScript Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Ajax Hacks," published by O'Reilly. We...
     

    Buy this book now. This article is excerpted from hack 58 of the book Ajax Hacks, written by Bruce W. Perry (O'Reilly; ISBN: 0596101694). Check it out today at your favorite bookstore. Buy this book now.

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







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