# How to fill select box with the names of the states corresponding to the country name selected in d

**URL:** <https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972>\
**Category:** Rails\
**Created:** [December 3, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972 "2007-12-03T15:06:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 3, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972/1 "2007-12-03T15:06:04Z")

</div>

hi all ,

can any body guide me, i have to fill the dropdown box with the name  
of the states of the country at run time which is dependent on the  
name of the country you selected.

is their any helper exists for this.

thanks  
sachin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 3, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972/2 "2007-12-03T15:43:08Z")

</div>

Hi Sachin,

Something like this:

in your controller

@states = State.find\_by\_country(params[:country])

then in your view

\<%= form.collection\_select :state\_id, @states, :id, :state %\>

This example is based on form\_for, so if you are using form\_tag,  
you’ll need the additional parameter at the beginning

\<%= collection\_select :person, :state\_id, @states, :id, :state %\>

Something like that anyway.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 3, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972/3 "2007-12-03T17:26:02Z")

</div>

Oy. I just realized that I didn’t read the message closely enough.  
I see now that you are needing to do one of two things:

1. post the form when the country is selected, use the code I  
mentioned earlier (or something similiar) and render the form again, or
2. use AJAX to query the database and repopulate the state select  
when the country changes.

I, personally, would go with #2. There is the argument that your app  
should degrade gracefully if the user doesn’t have JavaScript  
enabled, but I ignore that since my whole app depends on JavaScript  
and I doubt that very many people actually have it disabled these  
days. But that’s an architectural decision that I’ve made. Not  
everyone would be comfortable with it.

I can provide an example of #2 if you like.

Sorry for the seeming incompetence on my part. I’ll try to read more  
closely in the future.

Peace,  
Phillip

Begin forwarded message:

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2007, 7:41am UTC](https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972/4 "2007-12-05T07:41:25Z")

</div>

please forward me the example if it is possible for you thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2007, 7:49am UTC](https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972/5 "2007-12-05T07:49:36Z")

</div>

one more thing in your code what i realize is that i must have state  
table in my database, which contains the name of the states for the  
respective country or it is rails helper kind of thing

please confirm

thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2007, 10:54am UTC](https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972/6 "2007-12-05T10:54:05Z")

</div>

Use render :inline to populate the values for the state dropdown.

Summarize:

1. Post the AJAX request to the controller by using Observe\_field
2. Populate the state dropdown values using render :inline. You can  
write the logic in the controller action where you posted the AJAX  
requset.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 5, 2007, 7:57am UTC](https://www.ruby-forum.com/t/how-to-fill-select-box-with-the-names-of-the-states-corresponding-to-the-country-name-selected-in-d/121972/7 "2007-12-05T07:57:38Z")

</div>

removed\_email\_address@domain.invalid wrote:

> one more thing in your code what i realize is that i must have state  
> table in my database, which contains the name of the states for the  
> respective country or it is rails helper kind of thing
> 
> please confirm
> 
> thanks

Probably, you can use observe\_field to post the ajax request when  
country is getting changed
