So, I''m trying to get on the Ajax train here.
I have a form with, among other things, 2 different select boxes. The
first select box is a list of authors (whose select value is their
id). The second drop box is going to be the books for the given
author... so the contents of the second box depend on what is selected
in the first.
I am trying to use the observe_field helper to watch the author field,
and update the book select box based on what is selected. Following
the documentation on
http://rails.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html,
my rhtml looks something like this:
<FORM METHOD="POST" name="review_form"
id="review_form"
ACTION="/reviews/create">
<%= observe_field( "author_id", { :update =>
"book_select", :frequency
=> 3, :url => { :action => "book_select" } } ) %>
<select NAME="author_id" >
<%= options_from_collection_for_select( @authors, "id",
"name" ) %>
</select>
<div id="book_select">
<select disabled NAME="book[id]">You Must Choose an Author
First
</div>
The contents of the rhtml for the action ajax is calling ( :action =>
"book_select" } are:
<select NAME="book[id]" size=1>
<OPTION SELECTED value="-1"> Choose a Book
<% for book in @books %>
<OPTION value="<%= book.id %>"> <%= book.title
%>
<% end %>
</select>
Where I of course define the instance variable @books in my
book_select action in the controller.
However, when I load the page (Firefox, WinXP), I get a javascript error:
"Error: Form is not defined"
The generated JS looks like this:
<script type="text/javascript">new
Form.Element.Observer(''author_id'',
3, function(element, value) {new Ajax.Updater(''book_select'',
''/reviews/book_select'', {parameters:value,
asynchronous:true})})</script>
I''m at a loss. Any suggestions? Thanks!
Matt
p.s. I am running on Rails 0.12.1