# Implementing countries and states drop down

**URL:** <https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295>\
**Category:** Rails\
**Created:** [October 25, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295 "2007-10-25T12:47:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 25, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/1 "2007-10-25T12:47:59Z")

</div>

Hi all,

I am implementing the functionality of cascading drop downs of countries  
and states. I want this to be done with Ajax and ruby so that the page  
dont get refreshed on selecting the country in first drop down.

Currently I have implemented it with java script and ruby. But the  
problem is the page get refreshed when i select the country from  
countries drop down. Can some one help me out in implementing this  
functionality.

Any help would be greatly appreciated.

-Regards  
Ruchita S…

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 25, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/2 "2007-10-25T13:27:48Z")

</div>

Ruchita S. wrote:

> Hi all,
> 
> I am implementing the functionality of cascading drop downs of countries  
> and states. I want this to be done with Ajax and ruby so that the page  
> dont get refreshed on selecting the country in first drop down.
> 
> Currently I have implemented it with java script and ruby. But the  
> problem is the page get refreshed when i select the country from  
> countries drop down. Can some one help me out in implementing this  
> functionality.
> 
> Any help would be greatly appreciated.
> 
> -Regards  
> Ruchita S…  
> Hi Ruchita,

In Ajax the main advantage is that we can refresh the page in parts ie,  
buy defining divisions in view file as

and updating only this div as :update =\> "some name"

But at this juncture what I feel is that it is not due to abscence of  
Ajax but it is due to your code logic that the page is refreshing while  
selecting country only. Any way may be I can help you if you can send me  
some code especially that part where you implement drop down list

All the Best

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 25, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/3 "2007-10-25T13:33:02Z")

</div>

Abhi M. wrote:

> Ruchita S. wrote:
> 
> > Hi all,
> > 
> > I am implementing the functionality of cascading drop downs of countries  
> > and states. I want this to be done with Ajax and ruby so that the page  
> > dont get refreshed on selecting the country in first drop down.
> > 
> > Currently I have implemented it with java script and ruby. But the  
> > problem is the page get refreshed when i select the country from  
> > countries drop down. Can some one help me out in implementing this  
> > functionality.
> > 
> > Any help would be greatly appreciated.
> > 
> > -Regards  
> > Ruchita S…  
> > Hi Ruchita,
> 
> In Ajax the main advantage is that we can refresh the page in parts ie,  
> buy defining divisions in view file as
> 
> and updating only this div as :update =\> "some name"
> 
> But at this juncture what I feel is that it is not due to abscence of  
> Ajax but it is due to your code logic that the page is refreshing while  
> selecting country only. Any way may be I can help you if you can send me  
> some code especially that part where you implement drop down list
> 
> All the Best

Hi Abhi M.,

The part of the code where i am implementing it as follows. Can you plz  
tell me where is the problem.

 First Name Last Name Country  
 \<% @countries.each do |country| %\> \> \<%= country.country\_name %\> \<% end %\>   
  
\<% @rubyVar=request.parameters[“categoryid”] %\>  
\<input type =“text” id=“categorytxt” name=“textbox1” size=“30” value=  
\<%= @rubyVar %\> \> State \<% @states.each do |state| %\> \<% if (state.country\_id == (@rubyVar.to\_i)) %\> \> \<%= state.state\_name %\> \<% end %\> \<% end %\> City  
 Zip Code  

Thanks,  
Ruchita S.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 25, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/4 "2007-10-25T13:41:04Z")

</div>

Hi Ruchita

In your code you are telling like  
  
ie whenever there is a change in country dropdown selection list control  
should go to function goToSub() which inturn will load the other part  
rt?. So that is why it is getting refreshed

I will check some solution and get back to you

Thanks  
Abhimanyu

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 25, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/5 "2007-10-25T13:47:35Z")

</div>

Abhi M. wrote:

> Hi Ruchita
> 
> In your code you are telling like  
>   
> ie whenever there is a change in country dropdown selection list control  
> should go to function goToSub() which inturn will load the other part  
> rt?. So that is why it is getting refreshed
> 
> I will check some solution and get back to you
> 
> Thanks  
> Abhimanyu

Yeah… In function goToSub(); I am fetching the value of country id.  
window.location="…/user/new\_client?categoryid=" +  
category2.options[category2.selectedIndex].value;

Then i am getting this id by using this line of code:

\<% @rubyVar=request.parameters[“categoryid”] %\>

In the states drop down i am comparing:

if (state.country\_id == (@rubyVar.to\_i))  
then corresponding to this, states are displayed.

The problem is whenever I select the country, the fields which are above  
the country are lost perhaps due to refreshing of page.

Thanks,  
Ruchita S.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 25, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/6 "2007-10-25T13:52:35Z")

</div>

Abhi M. wrote:

> Abhi M. wrote:
> 
> > Hi Ruchita
> > 
> > In your code you are telling like  
> >   
> > ie whenever there is a change in country dropdown selection list control  
> > should go to function goToSub() which inturn will load the other part  
> > rt?. So that is why it is getting refreshed
> > 
> > I will check some solution and get back to you
> > 
> > Thanks  
> > Abhimanyu
> 
> Hi Ruchita
> 
> Sorry I have to reach some place. so I will post you tommorrow only.  
> Also try to use Collection\_select instead of “option” to show drop down  
> list so that you can include many options. check some tutorials on  
> collection\_select if you dont know that or else I will tell you tomorrow
> 
> Thanks
> 
> Abhimanyu

Hi

Thanks a lot. Not an issue but please do help me out.

-Ruchita

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 4:37am UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/7 "2007-10-26T04:37:19Z")

</div>

Hi

\<%= collection\_select("country","name",@countries,"id","name",{:onchange =\> "javascript:selectedData(this.options[selectedIndex].value)"}) %\>

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 25, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/8 "2007-10-25T13:50:22Z")

</div>

Abhi M. wrote:

> Hi Ruchita
> 
> In your code you are telling like  
>   
> ie whenever there is a change in country dropdown selection list control  
> should go to function goToSub() which inturn will load the other part  
> rt?. So that is why it is getting refreshed
> 
> I will check some solution and get back to you
> 
> Thanks  
> Abhimanyu

Hi Ruchita

Sorry I have to reach some place. so I will post you tommorrow only.  
Also try to use Collection\_select instead of “option” to show drop down  
list so that you can include many options. check some tutorials on  
collection\_select if you dont know that or else I will tell you tomorrow

Thanks

Abhimanyu

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 4:42am UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/9 "2007-10-26T04:42:10Z")

</div>

Abhi M. wrote:

> Hi
> 
> \<%= collection\_select("country","name",@countries,"id","name",{:onchange =\> "javascript:selectedData(this.options[selectedIndex].value)"}) %\>

function selectedData(selectedvalue)  
{  
var codeid = selectedvalue;  
\<%= remote\_function(:update =\> ‘show’, :url =\>  
showdescription\_url,:with =\> “‘value=’+encodeURIComponent(codeid)”)%\>  
}

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 4:46am UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/10 "2007-10-26T04:46:47Z")

</div>

Abhi M. wrote:

> Abhi M. wrote:
> 
> > Hi
> > 
> > \<%= collection\_select("country","name",@countries,"id","name",{:onchange =\> "javascript:selectedData(this.options[selectedIndex].value)"}) %\>

function selectedData(selectedvalue)  
{  
var stateid = selectedvalue;  
\<%= remote\_function(:update =\> ‘show’, :url =\>  
showstate\_url,:with =\> “‘value=’+encodeURIComponent(stateid)”)%\>  
}

you have to use roots.rb  
then shoud have a div called show in the view file where you specify the  
drop down list of countries

\<%=render:partial =\> states\> \<\div\>

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 6:27am UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/11 "2007-10-26T06:27:21Z")

</div>

Abhi M. wrote:

> Abhi M. wrote:
> 
> > Abhi M. wrote:
> > 
> > > Hi
> > > 
> > > \<%= collection\_select("country","name",@countries,"id","name",{:onchange =\> "javascript:selectedData(this.options[selectedIndex].value)"}) %\>
> 
> function selectedData(selectedvalue)  
> {  
> var stateid = selectedvalue;  
> \<%= remote\_function(:update =\> ‘show’, :url =\>  
> showstate\_url,:with =\> “‘value=’+encodeURIComponent(stateid)”)%\>  
> }
> 
> you have to use roots.rb  
> then shoud have a div called show in the view file where you specify the  
> drop down list of countries
> 
> \<%=render:partial =\> states\> \<\div\>
> 
> Hi,
> 
> Can you gmmme full code. Am not able to implement it in my code.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 8:10am UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/12 "2007-10-26T08:10:36Z")

</div>

Hi,  
I have given only some idea to you. Its not part of any code . You can  
use collection select for drop down list  
and if any country is selected then javascript function function  
selectedData(selectedvalue) is called. In that function itself it will  
call other action

function selectedData(selectedvalue)  
{  
var stateid = selectedvalue;  
\<%= remote\_function(:update =\> ‘show’, :action =\>  
action\_name,:with =\> “‘value=’+encodeURIComponent(stateid)”)%\>  
}

write the state drop down list in a partial file and render that file in  
the above action you call

and also define a div called show in the view file of country drop down  
and render the partial file here also.

Please try this

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 8:30am UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/13 "2007-10-26T08:30:42Z")

</div>

Abhi M. wrote:

> Hi,  
> I have given only some idea to you. Its not part of any code . You can  
> use collection select for drop down list  
> and if any country is selected then javascript function function  
> selectedData(selectedvalue) is called. In that function itself it will  
> call other action
> 
> function selectedData(selectedvalue)  
> {  
> var stateid = selectedvalue;  
> \<%= remote\_function(:update =\> ‘show’, :action =\>  
> action\_name,:with =\> “‘value=’+encodeURIComponent(stateid)”)%\>  
> }
> 
> write the state drop down list in a partial file and render that file in  
> the above action you call
> 
> and also define a div called show in the view file of country drop down  
> and render the partial file here also.
> 
> Please try this

Okie… let me try to implement this. Thanks for your help. I’ll get  
back to you.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/14 "2007-10-26T13:31:56Z")

</div>

Hi

For that you have to have associations between the users table and  
countries as well as states table

Use the country id and state id as foreign keys in users table

By the way did you use collection\_select to implement dropdrown .Did you  
solve the previous problem  
Please let me know

All the Best

Abhimanyu

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/15 "2007-10-26T13:36:18Z")

</div>

Abhi M. wrote:

> Hi
> 
> For that you have to have associations between the users table and  
> countries as well as states table
> 
> Use the country id and state id as foreign keys in users table
> 
> By the way did you use collection\_select to implement dropdrown .Did you  
> solve the previous problem  
> Please let me know
> 
> All the Best
> 
> Abhimanyu

Hi Abhimanyu

See I have a users table, countries table and a states table. I am  
taking foreign key country\_id and state\_id in users table.

I hav used collection\_select to implement the drop downs. Shall I send  
you the code to make it more clear?

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/16 "2007-10-26T13:45:11Z")

</div>

Hi Ruchita

Yes Please send me the code. You can use the foreign keys as you  
sent.Some times I wont be able to reply today . May be between morning  
and evening we can have discussion.

Are you a student . I asked because If you are free other times we can  
have discussion then. I will be leaving after a short while  
THANKS  
ABHIMANYU

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/17 "2007-10-26T12:45:55Z")

</div>

Ruchita S. wrote:

> Abhi M. wrote:
> 
> > Hi,  
> > I have given only some idea to you. Its not part of any code . You can  
> > use collection select for drop down list  
> > and if any country is selected then javascript function function  
> > selectedData(selectedvalue) is called. In that function itself it will  
> > call other action
> > 
> > function selectedData(selectedvalue)  
> > {  
> > var stateid = selectedvalue;  
> > \<%= remote\_function(:update =\> ‘show’, :action =\>  
> > action\_name,:with =\> “‘value=’+encodeURIComponent(stateid)”)%\>  
> > }
> > 
> > write the state drop down list in a partial file and render that file in  
> > the above action you call
> > 
> > and also define a div called show in the view file of country drop down  
> > and render the partial file here also.
> > 
> > Please try this
> 
> Okie… let me try to implement this. Thanks for your help. I’ll get  
> back to you.

Thanks a lot for your help. I have implemented the drop downs. Now the  
problem is I have to save the data of the country and state in the users  
table. I dont know how to save the country and state in the database  
because i am fetching the data from the countries and states table but i  
have to make it save in users table.

Please do let me know.  
Ruchita  
Thanks

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/18 "2007-10-26T13:55:36Z")

</div>

Ruchita S. wrote:

Hi,

I mean i am not able to save data in users table.

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/19 "2007-10-26T13:50:25Z")

</div>

Abhi M. wrote:

> Hi Ruchita
> 
> Yes Please send me the code. You can use the foreign keys as you  
> sent.Some times I wont be able to reply today . May be between morning  
> and evening we can have discussion.
> 
> Are you a student . I asked because If you are free other times we can  
> have discussion then. I will be leaving after a short while  
> THANKS  
> ABHIMANYU

Hi

 \<%= collection\_select(:country, :country\_name, @countries, :id, :country\_name, options = {:prompt =\> "â€“ Select a Country â€“"}, html\_options = { :onChange =\> "new Ajax.Updater('dd\_cities', '/user/country\_changed/' + this[this.selectedIndex].value, {asynchronous:true, evalScripts:true});" } ) %\> State \<%= render :partial =\> "states", :locals =\> { :country\_id =\> @countries[0].id} %\>

This is what i have written code in controller. I have made a  
\_states.rhtml.  
Now i am able to save the data in my users table.

I am not a student. Tomorrow wud be my off, so plz if p’ble do let me  
knw wat needs to be done?

Thanks,  
Ruchita S…

---

<div class="post-metadata">

**Author:** ![ruchita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/22d042/32.png) [@ruchita](https://www.ruby-forum.com/u/ruchita)\
**Post date:** [October 26, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295/20 "2007-10-26T15:00:08Z")

</div>

Ruchita S. wrote:

> Ruchita S. wrote:
> 
> Hi,
> 
> I mean i am not able to save data in users table.

Hi,

I am getting the following error when i click on save button:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.inject

Extracted source (around line #31):

28:

Country  
  
29:   
30: \<%=  
31: collection\_select(:country, :country\_name, @countries, :id,  
:country\_name,  
32: options = {:prompt =\> “ï¿½ Select a Country ï¿½”},  
33: html\_options =  
34: {

Do let me know what needs to be done?

Thanks  
Ruchita S…

[Next page](https://www.ruby-forum.com/t/implementing-countries-and-states-drop-down/118295.md?page=2)
