JavaScript
  Home arrow JavaScript arrow Page 4 - 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 - A Little Partial Pizzazz


    (Page 4 of 4 )

    Thepartialneeds thearrayvalues to build theselectlist; these are stored in theteamArrvariable. Rails uses its built-in naming convention forpartials (the underscore requirement) to find the correct content and render it as the response. Let’s look at thepartial_options.rhtml file:

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

    A little embedded Ruby code gives theselectelement itsidandname. The embedded code then iterates through thearray(passed into thepartialby therender()method) and creates anoptionelement for eacharraymember, as in<option>hockey</option>. Although a little server-side Ruby code was involved, the code did not have to touch the request object or deal with fetching or massaging the return value.

    Cleanup Code

    The only bit of trickery involved is making sure that the application does not append one newselect list after another inside thedivas the user clicks the button. We want all the existingselects in thatdivto be replaced by any new ones. Therefore, we include a little cleanup code that empties thedivbefore it’s updated with a newselect list:

      <%= form_remote_tag(:update => "sel_con",:url => { :action =>
    :create_select
      },
      :position => "top",success => "$('sel_con').innerHTML=''" ) %>

    successsequentially precedes thecompletestage, so this code sets thediv’s content to the emptystring before the newselect list appears.

    To handle any request failures, such as a downed web server, add this code from a prior hack as aform_remote_tag()parameter:

      #$('failure') is Prototype's shortcut for
      #document.getElementById('failure')
      :failure => "$('failure').innerHTML='Failure;
      request status='+request.status"
      


    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.

       · 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

    - 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
    - Active Client Pages at the Server
    - ACP Tab Web Page
    - Finishing a Slide Show Application with jQue...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT