JavaScript
  Home arrow JavaScript arrow Page 2 - 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 
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? 
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 - Work with Ajax and Ruby on Rails


    (Page 2 of 4 )

    In a Rails application, a controller component takes charge of the views that the web user sees. The controller component is located inside the <web-app-root>/app/controllers directory. Using http://localhost:3000/hacks/ as an example, the framework looks in <web-app-root>/app/ controllers/hacks_controller.rb for an action or method namedindexthat generates the response. Actions start out as Ruby methods within a controller object (a class, in object-oriented terms) defined in this file.

    The entire path on my Mac OS X machine is ~/Rails/Energy/app/controllers/ hacks_controller.rb. Energy is the top-level directory of the web application.

    Let’s take a look at hacks_controller.rb for theindex()method:

      class HacksController < ApplicationController
         
    def index
         
    end
     
    #rest of class definition...
      end

    Nothing happening there; it’s an empty method definition. In this case, Rails looks for a template named index.rhtml in the app/views/hacks directory.

    One of the intuitive aspects of the Rails framework is that the framework maps URL path information, such as the hacks part of http://localhost:3000/hacks, to directories of the same name in sensible locations, such as views.

    index.rhtml provides the template for our hack:

      <html>
      <head>
         
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
         
    <%= javascript_include_tag :defaults
    %>
         
    <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>

    This template calls the form_remote_tag() method to update the div positioned beneath theformtag. This method wraps all of the Ajax- and request object–related functionality, updating thedivwith server data and positioning any more information on top of or before any existingdivcontent (as specified by the:position => "top"parameter). In other words, when the user clicks the Show Sports button, anXMLHttpRequestobject is created, and itssend()method sends a request to a Rails action namedcreate_select. This action is defined as a Ruby method in the hacks_controller.rb file we peeked at before.

    Thecreate_selectaction renders the HTTP response, which specifies the tags and content of a new pop-up orselectlist. Figure 7-10 shows the result of clicking the Show Sports button.


    Figure 7-10.  Voila, up pops a select list

    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

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





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