Forum: Ruby on Rails Order Values in a Select Box Using select

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.
mikej (Guest)
on 2008-10-16 13:37
(Received via mailing list)
Been trawling for a while and know there must be an easy answer to
this so reluctant to work round it.

My select box:

<%= form.select( :data, { "Yes" => "1", "No" => "0"},{:prompt => "–
Select –"}) %>

The problem is the data is ordered alphabetically so the drop down
gives the options no, yes:

<select id="my_id" name="my[data]" >
<option value="">– Select –</option>
<option value="0">No</option>
<option value="1">Yes</option>
</select>

instead of:

<select id="my_id" name="my[data]" >
<option value="">– Select –</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>

Any thoughts?

Many thanks,

Mike
tejo (Guest)
on 2008-10-16 14:49
(Received via mailing list)
<%= f.select( :data, { "Yes" => "1", "No" => "0"}.sort.reverse,
{:prompt => "–
  Select –"}) %>
Daniel B. (Guest)
on 2008-10-16 14:58
mikej wrote:
> Been trawling for a while and know there must be an easy answer to
> this so reluctant to work round it.
>
> My select box:
>
> <%= form.select( :data, { "Yes" => "1", "No" => "0"},{:prompt => "�
> Select �"}) %>
>
> The problem is the data is ordered alphabetically so the drop down
> gives the options no, yes:
>
> <select id="my_id" name="my[data]" >
> <option value="">� Select �</option>
> <option value="0">No</option>
> <option value="1">Yes</option>
> </select>
>
> instead of:
>
> <select id="my_id" name="my[data]" >
> <option value="">� Select �</option>
> <option value="1">Yes</option>
> <option value="0">No</option>
> </select>
>
> Any thoughts?
>
> Many thanks,
>
> Mike

Hashes don't guarantee any particular order.  Not sure if rails is
sorting the hash by key as you suggest.  Maybe try array of arrays.
Ordinarily I'd expect the order to be maintained since its an array, but
I'm not sure:

  form.select :data , [['yes','1'],['no','0']] , ....

Regards,
Daniel
mikej (Guest)
on 2008-10-16 15:08
(Received via mailing list)
Splendid,
thanks.
This topic is locked and can not be replied to.