# State select box to be populated according to country

**URL:** <https://www.ruby-forum.com/t/state-select-box-to-be-populated-according-to-country/183474>\
**Category:** Rails\
**Created:** [February 15, 2010, 10:18am UTC](https://www.ruby-forum.com/t/state-select-box-to-be-populated-according-to-country/183474 "2010-02-15T10:18:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [February 15, 2010, 10:18am UTC](https://www.ruby-forum.com/t/state-select-box-to-be-populated-according-to-country/183474/1 "2010-02-15T10:18:16Z")

</div>

Hi  
I have the models company,country, state  
company belongs\_to country  
country has\_many states  
state belongs\_to country

```
   In my company creation view i have two select boxes for country

```

and state. I could implement the country select like

## \<% form\_for @company do |f| %\> \<%= f.collection\_select :country\_id, Country.all, :id,:name,{},{} %\>

```
  Upon submit country is save with company .This is working. Now I

```

dont know how to populate the state And also I have to change the state  
select box according to change in country.Please guide me

Thanks  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [February 15, 2010, 1:11pm UTC](https://www.ruby-forum.com/t/state-select-box-to-be-populated-according-to-country/183474/2 "2010-02-15T13:11:42Z")

</div>

Hi

> Ryan B. got a railscasts episode that deals with this exact case:  
> [#88 Dynamic Select Menus - RailsCasts](http://railscasts.com/episodes/88-dynamic-select-menus)  
> Thanks for your reply. I have tried this But is not working My rails  
> version is 2.3.5 I have added map.connect  
> ‘:controller/:action/:id.:format’  
> to routes.r as suggested. But in any way I cant access  
> [http://localhost:3001/javascripts/dynamic\_states.js](http://localhost:3001/javascripts/dynamic_states.js)  
> I am getting

Routing Error

No route matches “/javascripts/dynamic\_states.js” with {:method=\>:get}

Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [February 15, 2010, 10:57am UTC](https://www.ruby-forum.com/t/state-select-box-to-be-populated-according-to-country/183474/3 "2010-02-15T10:57:16Z")

</div>

Ryan B. got a railscasts episode that deals with this exact case:

> **[\#88 Dynamic Select Menus - RailsCasts](http://railscasts.com/episodes/88-dynamic-select-menus)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [February 15, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/state-select-box-to-be-populated-according-to-country/183474/4 "2010-02-15T19:29:38Z")

</div>

You need to but dynamic\_states.js in your public/javascripts folder.

–Lasse

2010/2/15 Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [February 16, 2010, 5:28am UTC](https://www.ruby-forum.com/t/state-select-box-to-be-populated-according-to-country/183474/5 "2010-02-16T05:28:25Z")

</div>

On Feb 15, 1:18 am, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi  
> I have the models company,country, state

You might avoid a lot of work by using the Carmen plugin (or one like  
it):

> **[GitHub - carmen-ruby/carmen: A repository of geographic regions for Ruby](https://github.com/carmen-ruby/carmen)**
>
> A repository of geographic regions for Ruby. Contribute to carmen-ruby/carmen development by creating an account on GitHub.

* * *

Kurt W.  
I am looking for a new rails job:  
[http://www.CircleW.org/kurt/pages/resume](http://www.CircleW.org/kurt/pages/resume)

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [February 16, 2010, 4:33am UTC](https://www.ruby-forum.com/t/state-select-box-to-be-populated-according-to-country/183474/6 "2010-02-16T04:33:05Z")

</div>

Lasse B. wrote:

> You need to but dynamic\_states.js in your public/javascripts folder.
> 
> –Lasse

But how is it possible? In screencast it is dynamic\_states.js.erb  
that contains both ruby and javascript code. So how can i put that to  
public/javascripts?

Tom
