Dynamically Generate a Selection List in a Rails Template
(Page 1 of 4 )
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.
Hack 58 Generate a selection list from server-side data using Ajax and Ruby on Rails.
This hack creates aselectelement from server-side data using a Ruby on Rails application. Unlike the typical manner in which a web widget is generated in a web page, the user chooses the content for the pop-up, and then the pop-up appears out of the blue, without anything else on the page changing. The selections that the user sees in the pop-up derive from a server-side component; they are not just hard-coded JavaScript or HTML that is part of the web page.
This hack has the same behavior as some of our earlier hacks, but the path our code takes to produce its effects is quite different. A Rails application wraps all of theXMLHttpRequestmechanics behind its own methods. Figure 7-9 shows what the web page looks like. The user reaches this page by typing http://localhost:3000/hacks/ into a web browser’s location field.
We’re using the WEBrick server that comes with Ruby on Rails.

Figure 7-9. Cop your own pop-up
The user chooses either Team or Individual from the pop-up and clicks the Show Sports button, and anotherselectlist (whose values are determined by the user’s choice in the first list) appears just above the button.
Next: Work with Ajax and Ruby on Rails >>
More JavaScript Articles
More By O'Reilly Media
|
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.
|
|