Dynamically Generate a Selection List in a Rails Template (Page 1 of 4 )
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.