Forum: Ruby on Rails Populating a select field using observe_field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
34c237452690b8df5123231a5b998575?d=identicon&s=25 Geoff Waggott (geoffw)
on 2006-01-19 06:53
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:

<p><label for="wine_country_id">Country</label><br/>
<%= 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' %>
</p>
<p><label for="wine_region_id">Region</label><br/>
<%= select 'wine', 'region_id', @wine.country.regions.collect {|r|
[r.name, r.id]} %>
</p>

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
34c237452690b8df5123231a5b998575?d=identicon&s=25 Geoff Waggott (geoffw)
on 2006-01-20 11:22
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
This topic is locked and can not be replied to.