Monday, March 26, 2007

AJAX state_select

After downloading the state_select plugin for rails.
I put in a little extra work to make the state select AJAX updated.
The address form looks like:

<%= state_select 'address', 'state', country='US' %>

...
<%= country_select 'address', 'country' %>
<%= observe_field :address_country, :frequency=>0.5,
:update=>"state_select",
:url=> {:action=>'state_select', :only_path=>false},
with=>"'country=' + encodeURIComponent(value)" %>

Where the AJAX calls into a partial form:

<% if params[:country] == "United States" %>
<%= state_select 'address', 'state', country='US' %>
<% elsif params[:country] == "India" %>
<%= state_select 'address', 'state', country='INDIA' %>
<% elsif params[:country] == "Canada" %>
<%= state_select 'address', 'state', country='CANADA' %>
<% elsif params[:country] == "Australia" %>
<%= state_select 'address', 'state', country='AUSTRALIA' %>
<% elsif params[:country] == "Spain" %>
<%= state_select 'address', 'state', country='SPAIN' %>
<% elsif params[:country] == "Uganda" %>
<%= state_select 'address', 'state', country='UGANDA' %>
<% elsif params[:country] == "France" %>
<%= state_select 'address', 'state', country='FRANCE' %>
<% elsif params[:country] == "Germany" %>
<%= state_select 'address', 'state', country='GERMAN' %>
<% else %>
<%= text_field 'address', 'state', :class=>"text" %>
<% end %>

I have posted to their blog to see if the state_select could be updated so there was no translation needed between the "country" from select_country and the "country" parameter passed into the state_select. It would be also nice to return a text field if there is no state list for the given country.
This would mean the partial code would just be:

<%= state_select 'address', 'state', params[:country].uppercase %>

5 comments:

galtzo said...

1. There is a typo in your example code. In the AJAX call :with is missing the colon.

2. Great example! Thanks

mindtonic said...
This comment has been removed by the author.
mindtonic said...

Hello,

This is a wonderful plugin, exactly what I am looking for.

I do not understand the AJAX partial file. I placed the if/elsif code into my file, it is not generating the drop downs even when I choose a proper country.

Perhaps you could point me to a resource with more detailed explanation on how to use this type of plugin? I would really appreciate it!

Thanks!

Maged Makled said...

Hey man,
I’m trying to have a selected state and a none option to the state_select. I wasn’t successful getting that to work. I tried everything but no luck. could you give me an example please.

Thanks

None said...

To add a none option, you would not need to modify any of the ajax code, you would need to modify the state_select.rb code. Add a none option to the top of the US array list.
Change the line:
US_STATES = [["Alabama", "AL"], ["Alaska", "AK"], ["A....
to start with:
US_STATES = [["None",""], ["Alabama", "AL"], ["Alaska", "AK"], ["A...
or any other state line you like. That should do the trick.