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
--
Posted via http://www.ruby-forum.com/.
Geoff Waggott
2006-Jan-20 09:45 UTC
[Rails] Re: Populating a select field using observe_field
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 -- Posted via http://www.ruby-forum.com/.