Forum: Ruby on Rails How to sort select tag options?

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.
Jeba M. (Guest)
on 2009-02-18 08:33
Hi,
I have a select tag as :
<%= select(:feedbacks, :directedto , {"----Select
One----"=>"none","Business contact"=>"Business Contact","Technical
support"=>"Technical Support","Payment Help"=>"Payment Help"},{
:selected => "none"},
{:class=>"field", :style=>"width:192px;"}) %>

I want the select options in the drop down to appear in the same order
as I have written them in the code....
But...this does not happen..and the options appear in any random
order..!!!!
Can anyone please help me..as to how should I sort my select options..
Thank you...
MaD (Guest)
on 2009-02-18 09:34
(Received via mailing list)
problem is that you are using a hash, which have no guaranteed order.
try an array like this:

  <%= select(:feedbacks, :directedto, ([["----Select One----",
"none"], ["Business contact", "Business Contact"], ["Technical
support", "Technical Support"], ["Payment Help", "Payment Help"]]),
{:selected => "none"}, {:class=>"field", :style=>"width:192px;"}) %>
Jeba M. (Guest)
on 2009-02-18 09:42
MaD wrote:
> problem is that you are using a hash, which have no guaranteed order.
> try an array like this:
>
>   <%= select(:feedbacks, :directedto, ([["----Select One----",
> "none"], ["Business contact", "Business Contact"], ["Technical
> support", "Technical Support"], ["Payment Help", "Payment Help"]]),
> {:selected => "none"}, {:class=>"field", :style=>"width:192px;"}) %>

Thank You so much...
That did the trick...
Thanks again...
This topic is locked and can not be replied to.