# Populating a select field using observe\_field

**URL:** <https://www.ruby-forum.com/t/populating-a-select-field-using-observe-field/52801>\
**Category:** Rails\
**Created:** [January 19, 2006, 6:53am UTC](https://www.ruby-forum.com/t/populating-a-select-field-using-observe-field/52801 "2006-01-19T06:53:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![geoffw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@geoffw](https://www.ruby-forum.com/u/geoffw)\
**Post date:** [January 19, 2006, 6:53am UTC](https://www.ruby-forum.com/t/populating-a-select-field-using-observe-field/52801/1 "2006-01-19T06:53:23Z")

</div>

Hi,

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:

Country  
\<%= 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' %\>

Region  
\<%= 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  
request:

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

\<%= 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  
appreciated.

Thanks,  
Geoff

---

<div class="post-metadata">

**Author:** ![geoffw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@geoffw](https://www.ruby-forum.com/u/geoffw)\
**Post date:** [January 20, 2006, 11:22am UTC](https://www.ruby-forum.com/t/populating-a-select-field-using-observe-field/52801/2 "2006-01-20T11:22:13Z")

</div>

Here I am talking to myself again 😉

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.

Cheers,  
Geoff
