RJS in Internet Explorer to update a list box

Hi,

I’m trying some RJS to update a series of list boxes in which the user
selects a state, and the following list gets updated with a list of
counties, and the same for the next list of areas.

My code works perfectly (albeit a bit slow) on Firefox, but on Internet
Explorer it clears the list box (instead of filling it) and Netscape
shows all the counties cramped together on one instead of
multiple options.

This is the relevant code from the form:

Select a
state
Select a
county
<%= observe_field “county_state_id”, :url => {:action =>
‘update_counties_list’}, :with => ‘county_state_id’ %>
<%= observe_field “area_county_id”, :url => {:action =>
‘update_areas_list’}, :with => ‘area_county_id’ %>

And on the controller:

def update_counties_list
render :update do |page|
if params[‘county_state_id’].blank?
page.replace_html ‘address_area_id’, ‘Select a
county’
page.replace_html ‘colonia_municipio_id’, ‘Select a
state’
else
@state = State.find params[‘county_state_id’], :include
=> ‘counties’
page.replace_html ‘area_county_id’, ‘’

  • options_from_collection_for_select(@state.counties, ‘id’, ‘name’)
    end
    end
    end

Upon inspecting HTTP traffic it seems the JavaScript code is fine (it
works on Firefox after all), and neither IE nor Netscape show any error
whatsoever.

Weird thing is, on IE, if I open the RJS URL directly, sometimes it
works (showing the JS code), sometimes it doesn’t, showing an error
message saying it can’t download the url (the requested site is either
unavailable or cannot be found).

I’m on edge #4716 and I’ve tested on IE 6 & 7 beta 3.

I hope someone can help me :frowning:

Kind regards,
Ivan V.

On 10 Aug 2006, at 18:09, Ivan V. wrote:

Weird thing is, on IE, if I open the RJS URL directly, sometimes it
works (showing the JS code), sometimes it doesn’t, showing an error
message saying it can’t download the url (the requested site is either
unavailable or cannot be found).

I’m on edge #4716 and I’ve tested on IE 6 & 7 beta 3.

I hope someone can help me :frowning:

IE goes wonky when trying to replace in the inner elements of
tags. You’ll be better off replacing either the whole
using page.replace instead of page.replace_html or just
wrapping the select itself in a div or span element, so you can use
that in your page.replace_html. This last method works, I’m using it
in one of my apps. It’s not really the ideal solution for semantic
XHTML markup, but sometimes you need to bend the rules a bit if that
makes it work on all browsers.

Also, instead of rendering your element in your controller, use a
render :partial passing an object (so you can use the name of the
partial as a variable, all selects can be rendered using the same
partial), your code would be a lot more DRY.

Best regards

Peter De Berdt

why not try using an onchange handler in your select list, rather than
observe_field:

  <select id="user_country_id" name="user[country_id]"

onchange="<%= remote_function( :update => ‘locality’, :url => {
:action => :change_country }, :with => “‘user_country=’ +
$F(‘user_country_id’)”) %>">
<%= options_from_collection_for_select(@countries, ‘id’,
‘country_name’, @locality.id ) %>

Mike

Hi mike,

could you please explain the flow in detail for the below, I am new to
ruby on rails.

And also i explained my requirement. i have a table called “locations”
which holds the fields location_code and location_name. And i displayed
that location_name in list box. Also i have another table called
“userdetails” which has field called country_code. I want to update the
country_code in “userdetails” table based on the selection of list box
it fetches the location_code.

  <select id="user_country_id" name="user[country_id]"

onchange="<%= remote_function( :update => ‘locality’, :url => {
:action => :change_country }, :with => “‘user_country=’ +
$F(‘user_country_id’)”) %>">
<%= options_from_collection_for_select(@countries, ‘id’,
‘country_name’, @locality.id ) %>

regards
Senthil

Ivan V. wrote:

Hi,

I’m trying some RJS to update a series of list boxes in which the user
selects a state, and the following list gets updated with a list of
counties, and the same for the next list of areas.

I’ve posted some days ago (Ajax - Rails - Ruby-Forum)
a code that replaces, using RJS, select option values. See if it can be
useful for you.

page[‘field_id’].length=0
@your_object.each do |obj|
page << “$(‘field_id’).options[$(‘field_id’).length]=new
Option(‘#{escape_javascript(obj.to_s)}’,#{obj.id})”
end

Regards
Massimo
http://www.addsw.it

Hi Massimo,

I am updating the value in some other way, which seems to be quite
simple.

in controller:

@object=classname.find(:all).map {|l| [l.field_name, l.field_code]}

in view:

Country
<%= select(:object2, :field_code, @object1) %>

here object1 is a parent table and object2 is a child table. based the
change of the value in object1 the object2 will get updated.

Now problem is different the i want display the value for the selection
in seperate text field. ie., how to get the code for the selected name.

regards
Senthil