# Drop down boxes from table data

**URL:** <https://discuss.rubyonrails.org/t/drop-down-boxes-from-table-data/53352>\
**Category:** rubyonrails-talk\
**Created:** [August 2, 2010, 4:28pm UTC](https://discuss.rubyonrails.org/t/drop-down-boxes-from-table-data/53352 "2010-08-02T16:28:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 2, 2010, 4:28pm UTC](https://discuss.rubyonrails.org/t/drop-down-boxes-from-table-data/53352/1 "2010-08-02T16:28:19Z")

</div>

Hi

I have another question for how best to do this. Now I have been following the tutorial at: [http://www.tunaslut.com/learning-ruby-drop-down-lists-in-one-to-many-relationships](http://www.tunaslut.com/learning-ruby-drop-down-lists-in-one-to-many-relationships), however when I change the view over from

\<% @cities.each do |city| %\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h city.city\_name %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h city.country\_id %\>\</td\>

to:

\<% @cities.each do |city| %\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h city.city\_name %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h city.country.country\_name %\>\</td\>

The system breaks with the following error

NoMethodError in Cities#index

Showing app/views/cities/index.html.erb where line #12 raised:

undefined method `country\_name' for nil:NilClass Extracted source (around line #12):

when attempting to display data on cities

My questions are:

1. Is there an easier way of doing drop down boxed populated from a table? 2. is this foreign key assignment working? 3. is this a good way of doing FK asignments? is there a better way.

Please forgive me, I'm old school, more used to the SQL statements....

Thanks again.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 2, 2010, 5:22pm UTC](https://discuss.rubyonrails.org/t/drop-down-boxes-from-table-data/53352/2 "2010-08-02T17:22:50Z")

</div>

disruptive tech wrote:

> Hi
> 
> I have another question for how best to do this. Now I have been following the tutorial at: [http://www.tunaslut.com/learning-ruby-drop-down-lists-in-one-to-many-relationships](http://www.tunaslut.com/learning-ruby-drop-down-lists-in-one-to-many-relationships), however when I change the view over from
> 
> \<% @cities.each do |city| %\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h city.city\_name %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h city.country\_id %\>\</td\>
> 
> to:
> 
> \<% @cities.each do |city| %\> &nbsp;&nbsp;\<tr\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h city.city\_name %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%=h city.country.country\_name %\>\</td\>
> 
> The system breaks with the following error
> 
> NoMethodError in Cities#index
> 
> Showing app/views/cities/index.html.erb where line #12 raised:
> 
> undefined method `country\_name' for nil:NilClass

...which means that city.country is nil. You have to make it not be.

> Extracted source (around line #12):
> 
> when attempting to display data on cities
> 
> My questions are:
> 
> 1. Is there an easier way of doing drop down boxed populated from a table?

collection\_select

> 2. is this foreign key assignment working? 3. is this a good way of doing FK asignments? is there a better way.

I don't see any assignment in your code.

> Please forgive me, I'm old school, more used to the SQL statements....

What's that got to do with anything?

> Thanks again.

Best,
