# Drop Down Select List from model

**URL:** <https://www.ruby-forum.com/t/drop-down-select-list-from-model/159408>\
**Category:** Rails\
**Created:** [February 4, 2009, 10:27am UTC](https://www.ruby-forum.com/t/drop-down-select-list-from-model/159408 "2009-02-04T10:27:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@Sahil\_D](https://www.ruby-forum.com/u/Sahil_D)\
**Post date:** [February 4, 2009, 10:27am UTC](https://www.ruby-forum.com/t/drop-down-select-list-from-model/159408/1 "2009-02-04T10:27:43Z")

</div>

hi all,

i am new to rails and not able to figure how to get a drop-down  
selection list in a view.  
i have 3 models (tables): country, state & city.  
country has many states  
state has many cities

now i want to create 3 drop down lists, 1 for each. selecting a  
country should make states available in that country only, and same  
for the state and cities.

i am trying to create the ‘country’ list with this:  
\<%= collection\_select(:state, :countries\_id,  
@countries, :id, :cou\_name, {:prompt =\> “select country”}) %\>

def select  
@countries = Country.find(:all)

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @countries }
end

```

end

although the box is shown, but nothing comes up in it.  
i am trying to do this inside a view for ‘state’. do i need to do it  
inside a separate controller/view??

plz help!!

---

<div class="post-metadata">

**Author:** ![Sahil\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@Sahil\_D](https://www.ruby-forum.com/u/Sahil_D)\
**Post date:** [February 4, 2009, 10:50am UTC](https://www.ruby-forum.com/t/drop-down-select-list-from-model/159408/2 "2009-02-04T10:50:05Z")

</div>

You need either javascript code, or an observer. This is not trivial.  
Start simply, one step at a time.

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![Sahil\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@Sahil\_D](https://www.ruby-forum.com/u/Sahil_D)\
**Post date:** [February 5, 2009, 6:22am UTC](https://www.ruby-forum.com/t/drop-down-select-list-from-model/159408/3 "2009-02-05T06:22:21Z")

</div>

You seem to be complicating things. I think this is what you need

\<%= collection\_select(:state, :country\_id, Country.find  
(:all), :id, :cou\_name, {:prompt =\> “select country”}) %\>

you do not need the “select” method in the controller for this. Unless  
of course you WANT an Ajax call to be made for some reason, in which  
case, you need to explain more on what exactly you need.
