Forum: Ruby on Rails I think options_for_select is backwards

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.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 CSN (Guest)
on 2006-01-14 09:21
(Received via mailing list)
With a hash such as:

stuff = {'1'=>'desc 1', '2'=>'desc 2', '3'=>'desc 3'}

options_for_select(stuff) will output:

<option id="desc 1">1</option>
<option id="desc 2">2</option>
<option id="desc 3">3</option>

Doesn't this seem backwards? Hash keys correspond to
ids, and should be used for option ids IMO.

BTW, is there a simple way to swap a hash's keys and
values (keys become values and vice versa). Probably
with .each ?

csn

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-14 12:56
CSN wrote:
> With a hash such as:
>
> stuff = {'1'=>'desc 1', '2'=>'desc 2', '3'=>'desc 3'}
>
> options_for_select(stuff) will output:
>
> <option id="desc 1">1</option>
> <option id="desc 2">2</option>
> <option id="desc 3">3</option>
>

As a general rule, I would use an array to fill the options rather than
a hash.  Hashes are not guaranteed to return their contents in any
particular order so you might end up with your options in a seemingly
random order.  Arrays will not do that.

[["desc 1", "1"],["desc 2", "2"],["desc 3", "3"]]

_Kevin
This topic is locked and can not be replied to.