Forum: Ruby on Rails country listbox from table

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John G. (Guest)
on 2007-07-31 20:45
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?
Shandy N. (Guest)
on 2007-07-31 21:35
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.
Michael G. (Guest)
on 2007-07-31 23:14
(Received via mailing list)
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
(Guest)
on 2007-08-01 01:04
(Received via mailing list)
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...
This topic is locked and can not be replied to.