Country listbox from table


#1

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?


#2

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.


#3

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


#4

On Jul 31, 9:45 am, John G. 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://blog.codahale.com/2006/04/09/usps-countries-list-for-rails/