Home arrow JavaScript arrow Page 3 - Dynamically Generate a Selection List in a Rails Template
JAVASCRIPT

Dynamically Generate a Selection List in a Rails Template


If you're tired of the usual ways in which AJAX generates web widgets, read this article for something new. It lets the user choose the content of the pop-up. This article is excerpted from hack 58 of the book Ajax Hacks, written by Bruce W. Perry (O'Reilly; ISBN: 0596101694). 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: 5 stars5 stars5 stars5 stars5 stars / 3
July 31, 2008
TABLE OF CONTENTS:
  1. · Dynamically Generate a Selection List in a Rails Template
  2. · Work with Ajax and Ruby on Rails
  3. · Rendering the HTTP Response
  4. · A Little Partial Pizzazz

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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!


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

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 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials