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.
760e5c90000057a6860986b69b77c400?d=identicon&s=25 John Griffiths (indiehead)
on 2007-07-31 18: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?
C4bfcc81ac9281cb905f38e97e4d4e0b?d=identicon&s=25 Shandy Nantz (snantz)
on 2007-07-31 19: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.
Ae82cad40a0caca9c932d45c7a9eb3cd?d=identicon&s=25 Michael Glaesemann (Guest)
on 2007-07-31 21:14
(Received via mailing list)
On Jul 31, 2007, at 12:35 , Shandy Nantz 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 Glaesemann
grzm seespotcode net
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 gene.tani@gmail.com (Guest)
on 2007-07-31 23:04
(Received via mailing list)
On Jul 31, 9:45 am, John Griffiths <rails-mailing-l...@andreas-s.net>
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.