Populating a select field using observe_field



I’ve just started toying with Rails and AJAX, so forgive me if this is a
bit of a stupid question. I’m trying to populate a select field when the
value of another select field is changed. Looking through the API doc,
it seems like observe_field is what I need.

In my view I have:

<%= select 'wine', 'country_id', @countries.collect {|c| [c.name, c.id]} %> <%= observe_field 'wine_country_id', :url => { :controller => 'countries', :action => 'regionOptions' }, :update => 'wine_region_id', :with => '"id="+value' %>

<%= select 'wine', 'region_id', @wine.country.regions.collect {|r| [r.name, r.id]} %>

My intention is that the wine_region_id select is populated by the
result of the AJAX request initiated by changing the wine_country_id
select value. Here is the controller and view code for handling that

def regionOptions
@country = Country.find(params[:id])

<%= options_from_collection_for_select @country.regions, “id”, “name” %>

This looks like it is working OK, returning the option tags I expect.
However, when the wine_region_id select is repopulated only a single
text node containing the concatenated option display text is put in it.
What am I doing wrong?

Any pointers to the stupid newbie mistake I’ve made would be greatly



Here I am talking to myself again :wink:

This looks like a javascript issue. Setting the innerHTML property of a
select element doesn’t work as I would expect, it seems to convert the
markup into a text node stripping out the tags.

I’ll have to return the markup for the whole select element and use a
div as the update target.