# Country listbox from table

**URL:** <https://www.ruby-forum.com/t/country-listbox-from-table/110879>\
**Category:** Rails\
**Created:** [July 31, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/country-listbox-from-table/110879 "2007-07-31T18:45:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [July 31, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/country-listbox-from-table/110879/1 "2007-07-31T18:45:58Z")

</div>

sorry again,

i’m building a registration form, getting stumped on the country  
listbox.

any ideas how i could build a listbox for countries, taking it’s content  
from a data table?

i’d imagine i’d have to generate a data model for country, fill it in  
with all the current ISO spec countries; that i can do.

then tie that data model to the account controller.

can i use two model’s inside one controller/view?

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [July 31, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/country-listbox-from-table/110879/2 "2007-07-31T19:35:01Z")

</div>

Certaintly,

I did something similiar only with Hotels that where being read from a  
database:

\<%= select( “hotelmembership”, “membership”, Hotel.find\_by\_sql(“SELECT  
name, id FROM hotels ORDER BY name ASC”).collect {|h| [h.name, h.id]}, {  
:include\_blank =\> FALSE }) %\>

hotelmembership is the name of the table and membership is the name of  
the column for the Hotel in that table.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [July 31, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/country-listbox-from-table/110879/3 "2007-07-31T21:14:32Z")

</div>

On Jul 31, 2007, at 12:35 , Shandy N. wrote:

> \<%= select( “hotelmembership”, “membership”, Hotel.find\_by\_sql(“SELECT  
> name, id FROM hotels ORDER BY name ASC”).collect {|h| [h.name,  
> h.id]}, {  
> :include\_blank =\> FALSE }) %\>

It’s bad style to include database lookups in your view code. Pass  
the collection as an instance variable set in your controller, e.g.,

# in controller

def new  
@hotels = Hotel.find(:all, :order =\> :name).collect { |h| [h.name,  
h.id] }  
end

# in view

\<%= select(“hotelmembership”, “membership”, @hotels,  
{ :include\_blank =\> FALSE }) %\>

I think you should be able to use collection\_select instead

# in controller

def new  
@hotels = Hotel.find(:all, :order =\> :name)  
end

# in view

\<%= collection\_select(“hotelmembership”, “membership”,  
@hotels, :id, :name,  
{ :include\_blank =\> FALSE }) %\>

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [July 31, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/country-listbox-from-table/110879/4 "2007-07-31T23:04:43Z")

</div>

On Jul 31, 9:45 am, John G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> then tie that data model to the account controller.
> 
> ## can i use two model’s inside one controller/view?
> 
> Posted viahttp://www.ruby-forum.com/.

googling around cause i figured this must be already be a plugin or  
something:

[http://rails.techno-weenie.net/forums/1/topics/536?page=1](http://rails.techno-weenie.net/forums/1/topics/536?page=1)  
[http://blog.codahale.com/2006/04/09/usps-countries-list-for-rails/](http://blog.codahale.com/2006/04/09/usps-countries-list-for-rails/)
